如何编写 python 脚本来搜索网站 html 中的匹配链接

发布于 2024-08-23 22:38:00 字数 71 浏览 6 评论 0原文

我对 python 不太熟悉,必须编写一个脚本来执行许多功能。 基本上我仍然需要的模块是如何检查网站代码以匹配预先提供的链接。

I am not too familiar with python and have to write a script to perform a host of functions.
Basically the module i still need is how to check a website code for matching links provided beforehand.

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

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

发布评论

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

评论(3

恰似旧人归 2024-08-30 22:38:00

匹配链接是什么?他们的 HREF 属性?链接显示文字?
也许是这样的:

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re
import urllib2

doc = urllib2.urlopen("http://somesite.com").read()
links = SoupStrainer('a', href=re.compile(r'^test'))
soup = [str(elm) for elm in BeautifulSoup(doc, parseOnlyThese=links)]
for elm in soup:
    print elm

这将获取 somesite.com 的 HTML 内容,然后使用 BeautifulSoup 解析它,仅查找 HREF 属性以“test”开头的链接。然后它会构建这些链接的列表并将其打印出来。

您可以使用文档对其进行修改以执行任何操作。

Matching links what? Their HREF attribute? The link display text?
Perhaps something like:

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re
import urllib2

doc = urllib2.urlopen("http://somesite.com").read()
links = SoupStrainer('a', href=re.compile(r'^test'))
soup = [str(elm) for elm in BeautifulSoup(doc, parseOnlyThese=links)]
for elm in soup:
    print elm

That will grab the HTML content of somesite.com and then parse it using BeautifulSoup, looking only for links whose HREF attribute starts with "test". It then builds a list of these links and prints them out.

You can modify this to do anything using the documentation.

烟酉 2024-08-30 22:38:00

通常,您使用 urlliburllib2(htmllib 等)用于在 Python 中进行 Web 编程。您还可以使用mechanizecurl 等。然后,为了处理 HTML 并获取链接,您需要使用像 BeautifulSoup

Generally, you use urllib, urllib2 (htmllib etc) for programming web in Python. you could also use mechanize, curl etc. Then for processing HTML and getting links, you would want to use parsers like BeautifulSoup.

生寂 2024-08-30 22:38:00

尝试 scrapy,最全面的网页提取框架。

http://scrapy.org

try scrapy , the most comprehensive web extraction framework.

http://scrapy.org

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