Python HTML 抓取

发布于 2024-08-12 11:25:16 字数 257 浏览 2 评论 0原文

这并不是真正的抓取,我只是想在网页中找到该类具有特定值的 URL。例如:

<a class="myClass" href="/url/7df028f508c4685ddf65987a0bd6f22e">

我想获取href值。关于如何做到这一点有什么想法吗?也许正则表达式?你能发布一些示例代码吗? 我猜 html 抓取库,例如 BeautifulSoup,只是为了这个有点矫枉过正......

非常感谢!

It's not really scraping, I'm just trying to find the URLs in a web page where the class has a specific value. For example:

<a class="myClass" href="/url/7df028f508c4685ddf65987a0bd6f22e">

I want to get the href value. Any ideas on how to do this? Maybe regex? Could you post some example code?
I'm guessing html scraping libs, such as BeautifulSoup, are a bit of overkill just for this...

Huge thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

路弥 2024-08-19 11:25:16

正则表达式通常是一个坏主意,请尝试使用 BeautifulSoup

简单示例:

html = #get html
soup = BeautifulSoup(html)
links = soup.findAll('a', attrs={'class': 'myclass'})
for link in links:
    #process link

Regex is usally a bad idea, try using BeautifulSoup

Quick example:

html = #get html
soup = BeautifulSoup(html)
links = soup.findAll('a', attrs={'class': 'myclass'})
for link in links:
    #process link
白云悠悠 2024-08-19 11:25:16

啊,不是用于解析 HTML 的正则表达式

幸运的是,在 Python 中我们有 BeautifulSouplxml 为我们完成这项工作。

Aargh, not regex for parsing HTML!

Luckily in Python we have BeautifulSoup or lxml to do that job for us.

夜血缘 2024-08-19 11:25:16

正则表达式将是一个糟糕的选择。 HTML 不是常规语言。 美丽汤怎么样?

Regex would be a bad choice. HTML is not a regular language. How about Beautiful Soup?

渔村楼浪 2024-08-19 11:25:16

正则表达式不应用于解析 HTML。有关解释,请参阅此问题的第一个答案 :)

+1 为 BeautifulSoup。

Regex should not be used to parse HTML. See the first answer to this question for an explanation :)

+1 for BeautifulSoup.

浪荡不羁 2024-08-19 11:25:16

如果您的任务就是这么简单,那么

f=open("htmlfile")
for line in f:
    if "<a class" in line and "myClass" in line and "href" in line:
        s = line [ line.index("href") + len('href="') : ]
        print s[:s.index('">')]
f.close()

在这种情况下,不必使用字符串操作(甚至不需要正则表达式)HTML 解析器。

If your task is just this simple, just use string manipulation (without even regex)

f=open("htmlfile")
for line in f:
    if "<a class" in line and "myClass" in line and "href" in line:
        s = line [ line.index("href") + len('href="') : ]
        print s[:s.index('">')]
f.close()

HTML parsers is not a must for such cases.

云淡月浅 2024-08-19 11:25:16

问题是我知道 HTML 页面的结构,我只想找到特定类型的链接(其中 class="myclass")。无论如何,美丽汤?

The thing is I know the structure of the HTML page, and I just want to find that specific kind of links (where class="myclass"). BeautifulSoup anyway?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文