【问题标题】:Webscraping using python and beautifulsoup is not returning all requested data使用 python 和 beautifulsoup 进行网页抓取不会返回所有请求的数据
【发布时间】:2017-09-20 12:05:51
【问题描述】:

我正在尝试为所有 NSN 抓取此站点 https://www.iso-group.com/fsg/Mechanical-Power-Transmission-Equipment/Gears-Pulleys-Sprockets-Transmission-Chain/3020_GEAR-SPUR/1 使用以下代码

 import requests
 from bs4 import BeautifulSoup
 import urllib3

 from datetime import datetime, timedelta



from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

f = open("GEAR SPUR.csv", "w")

headers = "NSN, Part_Name, \n"
f.write(headers)

for page in range(69):
    NSNurl = 'https://www.iso-group.com/fsg/Gears-Pulleys-Sprockets-Transmission-Chain/Mechanical-Power-Transmission-Equipment/3020_GEAR-SPUR/'.format(page)

    uClient = requests.get(NSNurl)

    page_html = uClient.content

    # close client
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    container = page_soup.find_all("table", {"class": "table table-hover table-responsive"})


   for container1 in container:    #container1 is <td> element
       NSN = container1.find('td')
       nsn = container1.find('a') #get the anchor from the <td>
       if nsn is not None:        #if an anchor is found
          NSN = nsn.contents[0]
          print(nsn.contents[0]) #print it
       else:
          Nomenclature = container1.text
          print(NSN) 

          f.write(NSN + "," + Nomenclature.replace(",",""))
  f.close()

它只对第一个数字有效,并且一遍又一遍地返回相同的数字。这些是我终止它之前的输出

 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495
 3020-00-449-0495 - 3020004490495

为什么要这样做,我该如何解决它以返回所有数字?

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    您在这一行中遇到的另一个问题:

    container = page_soup.find_all("table", {"class": "table table-hover table-responsive"})

    您正在尝试遍历 container,但它的长度为 1。

    您可能应该像这样修改行:

    container = page_soup.find("table", {"class": "table table-hover table-responsive"}).find_all('tr')

    【讨论】:

      【解决方案2】:

      您的网址在字符串中没有用于您的页码的占位符。您需要在您希望页码所在的字符串中包含{},否则.format(page) 什么也不做。目前您的网址永远不会改变。根据您的评论,将您的网址更改为

      NSNurl = 'https://www.iso-group.com/fsg/Gears-Pulleys-Sprockets-Transmission-Chain/Mechanical-Power-Transmission-Equipment/3020_GEAR-SPUR/{}'.format(page)
      

      花括号充当占位符,当您使用.format 时,值将替换为占位符。查看here 了解有关格式化字符串的更多信息。

      我还会看到 Roman 关于抓取标记的回答

      您可能会遇到的另一个问题是您获取 NSN/nsn/Nomenclature 的实际代码有点难以破译,并且只有在 nsn is None 时才会实际保存到您的 csv,即使您正在打印 for两种情况。

      希望这足以让您修复代码。如果您仍有问题,请澄清问题。

      【讨论】:

      • 感谢您的快速回复。我在这里转移代码时忘记缩进以正确缩进代码。我现在已经复制了缩进。在页面所在的字符串中包含 {} 是什么意思?您是在谈论单独的页面声明吗?
      • @learner101 我已经更新了我的答案 - 您的 url 永远不会改变,您希望循环中的 0-68 在 url 文本中的哪个位置出现?
      • 是的。它在网址中。例如 NSNurl = 'iso-group.com/fsg/Gears-Pulleys-Sprockets-Transmission-Chain/… (然后是 3,4,5,6,....)
      • @learner101 尝试按照我的回答更改网址,看看您是否能走得更远。请务必考虑 Roman 关于如何正确解析标记的观点。
      猜你喜欢
      • 2020-04-20
      • 2016-02-21
      • 1970-01-01
      • 2022-12-17
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多