【问题标题】:How do I get the inspect element code instead of the page source when they are both different?当它们都不同时,如何获取检查元素代码而不是页面源?
【发布时间】:2022-01-24 03:31:10
【问题描述】:

我试图使用以下代码从this网站的inspect元素代码中获取所有链接。

import requests
from bs4 import BeautifulSoup

url = 'https://chromedriver.storage.googleapis.com/index.html?path=97.0.4692.71/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

for link in soup.find_all('a'):
    print(link)

但是,我没有链接。然后,我通过打印检查了soup 是什么,并将其与在实际网站上检查元素和查看页面源代码后得到的代码进行了比较。 print(source) 返回的代码与我单击查看页面源时显示的代码匹配,但它与单击检查元素时显示的代码不匹配。首先,如何获取检查元素代码而不是页面源代码?其次,为什么两者不同?

【问题讨论】:

  • 我不是网络爬虫方面的专家,但快速浏览一下就会发现该页面是由 javascript 生成的,经过几次搜索后,bs 似乎无法真正获得它,您需要像硒一样的东西
  • 该页面通过从 different url 获取和处理 XML 来工作。您可以尝试直接获取和处理该 XML。

标签: python web-scraping beautifulsoup python-requests get


【解决方案1】:

只需使用 cmets 中提到的 the other URL 并将 XML 解析为 BeautifulSoup

例如:

import requests
from bs4 import BeautifulSoup

url = "https://chromedriver.storage.googleapis.com/?delimiter=/&prefix=97.0.4692.71/"
soup = BeautifulSoup(requests.get(url).text, features="xml").find_all("Key")
keys = [f"https://chromedriver.storage.googleapis.com/{k.getText()}" for k in soup]
print("\n".join(keys))

输出:

https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_linux64.zip
https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_mac64.zip
https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_mac64_m1.zip
https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_win32.zip
https://chromedriver.storage.googleapis.com/97.0.4692.71/notes.txt

【讨论】:

    最近更新 更多