【发布时间】:2019-08-20 22:50:50
【问题描述】:
如果我访问此页面here,我可以在检查时看到带有img标签的页面上的图像。
但是当我尝试使用requests 获取页面并使用BeautifulSoup 进行解析时,我无法访问相同的图像。我在这里错过了什么?
代码运行良好,我从请求中得到 200 作为 status_code。
import requests
from bs4 import BeautifulSoup
url = 'https://mangadex.org/chapter/435396/2'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
page = requests.get(url,headers=headers)
print(page.status_code)
soup = BeautifulSoup(page.text,'html.parser')
img_tags = soup.find_all('img')
for img in img_tags:
print(img)
编辑::
根据建议,硒选项工作正常。但是有没有办法像 BeautifulSoup 那样加快速度?
【问题讨论】:
-
@SuperShoot 这确实按预期工作,但速度很慢。有更快的方法吗?喜欢,无需访问该页面。
-
PhantomJS会加快速度吗? -
通常如果页面是用 JavaScript 加载的,有一个 api 你可以直接调用 request 来获取 JSON 格式的数据。查看页面检查的网络选项卡,查看正在使用哪些资源将数据拉入页面。
-
@SuperShoot 非常感谢,分析网络调用给了我API请求。如何接受您的评论?
标签: python python-3.x web-scraping beautifulsoup