【发布时间】: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