在Python 3中使用BeautifulSoup抓取网址

TAN-CF-OK

我尝试了此代码,但带有URL的列表保持为空。没有错误的按摩,什么都没有。

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request('https://www.metacritic.com/browse/movies/genre/date?page=0', headers={'User-Agent': 'Mozilla/5.0'})
html_page = urlopen(req).read()

soup = BeautifulSoup(html_page, features="xml")
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^https://www.metacritic.com/movie/")}):
    links.append(link.get('href'))

print(links)

我要抓取在给定URL“ https://www.metacritic.com/browse/movies/genre/date中找到的所有以“ https://www.metacritic.com/movie/开头的URL吗? page = 0 “。

我究竟做错了什么?

莱罗皮

首先,您应该使用标准库“ html.parser”而不是“ xml”来解析页面内容。它可以更好地处理损坏的html(请参阅Beautiful Soup findAll找不到全部

然后看一下您正在解析的页面的源代码。您要查找的元素如下所示:<a href="/movie/woman-at-war">

因此,像这样更改代码:

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request('https://www.metacritic.com/browse/movies/genre/date?page=0', headers={'User-Agent': 'Mozilla/5.0'})
html_page = urlopen(req).read()

soup = BeautifulSoup(html_page, 'html.parser')
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^/movie/")}):
    links.append(link.get('href'))

print(links)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用不变的网址抓取多个页面-Python 3

在Python3中使用Selenium抓取动态表

使用Python Beautifulsoup进行抓取以获得作为链接的href的网址

无法使用python和beautifulsoup抓取网页中的某些href

在Python中抓取网址

在Python中浏览Selenium并使用BeautifulSoup进行抓取

在Python抓取脚本中请求多个网址

如何在Python 3中使用请求抓取数据绕过单选按钮?

在Python标签中使用BeautifulSoup进行网络抓取

使用python中的BeautifulSoup从网站抓取报告

抓取“ __hpKey”的网站,然后在python中使用请求和beautifulsoup登录

如何在python中使用BeautifulSoup抓取隐藏的表内容?

使用BeautifulSoup按Python中的元素抓取HTML

无法在python中使用scrapy抓取产品网址

使用BeautifulSoup Python抓取网页

如何使用scrapy在Python中抓取网址

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

使用嵌套 for 循环抓取网页,python3 中的 BeautifulSoup

使用beautifulsoup python抓取时解析div中的json对象

使用 BeautifulSoup 在 python 中抓取多个页面

使用 Python 3.7 中的 Beautifulsoup 从 WSJ 抓取网页文章?

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

如何使用beautifulsoup从python中的url中抓取数据

网页抓取python中打印网址的问题

从 Python Beautifulsoup 中抓取表格

使用 python 抓取网站 - BeautifulSoup

在 python 网页抓取中使用 Selenium 对 BeautifulSoup 进行分页

如何使用 Python 和 BeautifulSoup 从 html 表中抓取数据?

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4