如何提取在chrome开发人员工具上显示的href属性,但在BeautifulSoup的输出中不显示

问题描述 投票:0回答:1

我正在尝试使用Python的请求和bs4抓取一个网站来编译和摘要新闻。我尝试访问的链接(href)出现在Chrome的开发者工具中,其路径如下:

“ / html / body / div / div / div / main / article / div / div / section / div / section / div / div [3] / ul / li [1] / a”

我尝试了所有方法来提取它们,但是我意识到Python的html输出不会下降到该水平。它停留在:

“ / html / body / div / div / div / main / article / div / div / section / div / section”

我正在使用以下代码:

import requests
from bs4 import BeautifulSoup
url = 'https://www.gp.com/news'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
for url in soup.find_all('a'):
    print(url.get('href'))

非常感谢您能给我任何帮助,因为我完全没有想法。另外,我是编程的新手,所以希望您的回答尽可能精明。

非常感谢!

python beautifulsoup python-requests href screen-scraping
1个回答
0
投票

requests模块无法呈现javascript,您必须使用requests-htmlhttps://github.com/psf/requests-html)。如果在浏览器中打开页面,然后查看源代码(通常是CTRL-U),则可以看到区别。这将与您使用开发人员工具查看的内容有所不同,因为后者包含由javascript呈现的内容。

© www.soinside.com 2019 - 2024. All rights reserved.