我正在尝试抓取一个网站。我曾尝试使用两种方法,但都没有为我提供我正在寻找的完整网站源代码。我试图从下面提供的网站 URL 中抓取新闻标题。
网址:“https://www.todayonline.com/”
这是我尝试过但失败的两种方法。
方法一:美汤
tdy_url = "https://www.todayonline.com/"
page = requests.get(tdy_url).text
soup = BeautifulSoup(page)
soup # Returns me a HTML with javascript text
soup.find_all('h3')
### Returns me empty list []
方法二: Selenium +BeautifulSouptdy_url = "https://www.todayonline.com/"
options = Options()
options.headless = True
driver = webdriver.Chrome("chromedriver",options=options)
driver.get(tdy_url)
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html)
soup.find_all('h3')
### Returns me only less than 1/4 of the 'h3' tags found in the original page source
请帮忙。我试过抓取其他新闻网站,它更容易。谢谢你。
最佳答案
import requests
url = "https://www.todayonline.com/api/v3/news_feed/7"
data = requests.get(url).json()
关于python - 如何在 Python 中抓取 javascript 网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63761991/