【问题标题】:Data is missing while scraping using beautifulsoup4使用 beautifulsoup4 抓取时数据丢失
【发布时间】:2019-08-15 04:53:22
【问题描述】:

实际上,我是使用 Python Beautifulsoup4 进行解析的新手。我在刮this website。我需要首页上的当前每英里价格

我已经用了 3 个小时。在互联网上寻找解决方案时。我知道有一个库 PyQT4 可以像网络浏览器一样模仿并加载内容,然后一旦完成加载,您就可以提取所需的数据。但是我崩溃了。

使用这种方法以原始文本格式收集数据。我也尝试了其他方法。

def parseMe(url):
    soup = getContent(url)
    source_code = requests.get(url)
    plaint_text = source_code.text
    soup = BeautifulSoup(plaint_text, 'html.parser')
    osrs_text = soup.find('div', class_='col-md-12 text-center')
    print(osrs_text.encode('utf-8'))

Please have a look on this image。我认为问题在于 ::before 和 ::after 标签。它们会在页面加载后出现。
我们将不胜感激。

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup python-requests


    【解决方案1】:

    问题在于 javascript 会动态添加您要在该网站上抓取的数据。您可以尝试在客户端运行 JS,等待获取您想要抓取的数据,然后获取 DOM 内容 - 如果您想这样做,请查看 @gmds answer 这个问题。另一种方法是检查 javascript 代码发出的请求以及哪个请求包含您需要的信息。然后,您可以使用 python 发出该请求并获取所需的数据,而无需使用 PyQT4 甚至 BS4。

    【讨论】:

      【解决方案2】:

      正如其他答案所述,此页面仅包含文本 Current Price Per Mil:0USD。中间的值 - 0.8 - 是使用 JS 从下面描述的 url 动态获取的(可以获取 using a process described (for example) here and many other places。该站点检查机器人所以你有 to use a method described (for example) here

      所以大家一起来:

      url = 'https://api.boglagold.com/api/product/?id=osrs-gold&couponCode=null'
      import requests
      response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
      
      response.json()['sellPrice']
      

      输出:

      0.8
      

      【讨论】:

        【解决方案3】:

        网页制作了一个 XHR 来获取一个 JSON 文件,其中包含但价格

        import requests
        
        r = requests.get('https://api.boglagold.com/api/product/?id=osrs-gold&couponCode=null')
        j = r.json()
        # print(j)
        print('sellPrice', j['sellPrice'])
        print('buyPrice', j['buyPrice'])
        

        输出:

        sellPrice 0.8
        buyPrice 0.62
        

        【讨论】:

          【解决方案4】:

          你应该使用selenium 而不是`requests:

          from selenium import webdriver
          from bs4 import BeautifulSoup
          
          def parse(url):
              driver = webdriver.Chrome('D:\Programming\utilities\chromedriver.exe')
              driver.get('https://boglagold.com/buy-runescape-gold/')
              soup = BeautifulSoup(driver.page_source)
              return soup.find('h4', {'id': 'curr-price-per-mil-text'}).text
          
          parse()
          

          输出:

          'Current Price Per Mil: 0.80USD'
          

          原因是那个元素的值是通过JavaScript获取的,requests处理不了。这个特殊的 sn-p 代码使用 Chrome 驱动程序;如果您愿意,您可以使用 Firefox/其他一些等效的浏览器(您需要安装 selenium 库并自己查找 Chrome 驱动程序)。

          【讨论】:

            猜你喜欢
            • 2018-09-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-11
            • 1970-01-01
            • 2020-09-26
            • 2020-07-10
            相关资源
            最近更新 更多