【发布时间】:2021-09-03 06:06:11
【问题描述】:
我正在尝试抓取此页面 https://ntrs.nasa.gov/search 。 我正在使用下面的代码,当有更多标签时,Beautiful soup 只找到 3 个标签。我尝试过使用 html5lib、lxml 和 HTML 解析器,但它们都不起作用。
你能告诉我可能是什么问题吗?
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
# Set the URL
url = 'https://ntrs.nasa.gov/search'
# Connect to the URL
response = requests.get(url)
# Parse HTML and save to a BeautifulSoup object¶
soup = BeautifulSoup(response.content, "html5lib")
# soup = BeautifulSoup(response.text, "html5lib")
# soup = BeautifulSoup(response.content, "html.parser")
# soup = BeautifulSoup(response.content, "lxml")
# loop through all a-tags
for a_tag in soup.findAll('a'):
if 'title' in a_tag:
if a_tag['title'] == 'Download Document':
link = a_tag['href']
download_url = 'https://ntrs.nasa.gov' + link
urllib.request.urlretrieve(download_url,'./'+link[link.find('/citations/')+1:11])
【问题讨论】:
-
如果在浏览器中找不到带有 BS 的元素,则可能是页面正在使用 JavaScript 动态加载。见Web-scraping JavaScript page with Python。我建议使用
requests_html或selenium。
标签: python beautifulsoup