【问题标题】:{{link}} is getting returned while scraping URLs from a webpage using Python使用 Python 从网页中抓取 URL 时返回 {{link}}
【发布时间】:2020-07-16 17:16:22
【问题描述】:

我正在从如下网页中抓取网址

from bs4 import BeautifulSoup
import requests

url = "https://www.investing.com/search/?q=Axon&tab=news"
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(response.content, "html.parser")

for s in soup.find_all('div',{'class':'articleItem'}):

    for a in s.find_all('div',{'class':'textDiv'}):
        for b in a.find_all('a',{'class':'title'}):
            print(b.get('href'))

输出如下所示

/news/stock-market-news/axovant-updates-on-parkinsons-candidate-axolentipd-1713474
/news/stock-market-news/digital-alley-up-24-on-axon-withdrawal-from-patent-challenge-1728115
/news/stock-market-news/axovant-sciences-misses-by-009-763209
/analysis/microns-mu-shares-gain-on-q3-earnings-beat-upbeat-guidance-200529289
/analysis/axon,-espr,-momo,-zyne-200182141
/analysis/factors-likely-to-impact-axon-enterprises-aaxn-q4-earnings-200391393
{{link}}
{{link}}

问题是

  1. 未提取所有 URL
  2. 看到最后两条,为什么会这样?

以上两个问题有什么解决办法吗?

【问题讨论】:

  • 它在网站上无限加载,当您发出获取请求时,我只加载它的一部分,它在浏览器中就像这样,但是当您向下滚动更多页面加载时

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

那是因为您正在发出 HTTP 请求,而 youtube 使用 JavaScript 呈现视频数据。为了能够解析 JS 内容,你必须使用支持发出请求然后用 JS 渲染它的库。尝试使用模块requests_html。 pypi.org/project/requests-html

【讨论】:

  • youtube 在这里如何参与?
  • @Aiyaz 电影不是HTTP 序列。它们是一个JavaScript 加载序列,这意味着它们是通过脚本呈现的动态内容。
【解决方案2】:

解决此问题的一种方法是使用 selenium:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

当 selenium 向下滚动到页面底部时,您会阅读 pagesource 并关闭 selenium 并使用 Beautifulsoup 解析 pagesource。也可以用 selenium 解析到。

先硒和bs4:

from selenium import webdriver
from bs4 import BeautifulSoup

import time

PAUSE_TIME = 1
driver = webdriver.Firefox(executable_path='c:/program/geckodriver.exe')
driver.get('https://www.investing.com/search/?q=Axon&tab=news')
lh = driver.execute_script("return document.body.scrollHeight")

while True:

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


    time.sleep(PAUSE_TIME)


    nh = driver.execute_script("return document.body.scrollHeight")
    if nh == lh:
        break
    lh = nh
pagesourece = driver.page_source
driver.close()

soup = BeautifulSoup(pagesourece, "html.parser")

for s in soup.find_all('div',{'class':'articleItem'}):

    for a in s.find_all('div',{'class':'textDiv'}):
        for b in a.find_all('a',{'class':'title'}):
            print(b.get('href'))

只有硒版本:

from selenium import webdriver
from bs4 import BeautifulSoup

import time

PAUSE_TIME = 1
driver = webdriver.Firefox(executable_path='c:/program/geckodriver.exe')
driver.get('https://www.investing.com/search/?q=Axon&tab=news')
lh = driver.execute_script("return document.body.scrollHeight")

while True:

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


    time.sleep(PAUSE_TIME)


    nh = driver.execute_script("return document.body.scrollHeight")
    if nh == lh:
        break
    lh = nh
pagesourece = driver.page_source




for s in driver.find_elements_by_css_selector('div.articleItem'):

    for a in s.find_elements_by_css_selector('div.textDiv'):
        for b in a.find_elements_by_css_selector('a.title'):
            print(b.get_attribute('href'))
driver.close()

注意你必须安装selenium 并下载geckodriver 来运行它。如果您想在其他路径中使用 geckodriver,则必须更改 c:/program:

driver = webdriver.Firefox(executable_path='c:/program/geckodriver.exe')

到你的 geckodriver 路径。

【讨论】:

  • 我会试试这个 sn-p 并让你知道它是否适用于我的情况。谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 2016-10-14
  • 2021-02-25
相关资源
最近更新 更多