繁体   English   中英

Python 3.7.6 web 刮板写入 csv 截断结果

[英]Python 3.7.6 web scraper writing to csv truncates results

在这里输入图像描述初学者在这里。 我根据在网上找到的几个不同示例构建了这个刮板。 它正在连接和创建 csv 文件,没有明显问题,但文件比预期的要短得多。 eBay 显示 2k+ 结果,其中 200 出现在首页上,但 csv 文件 output 只有 55 行。 这 55 行似乎与 eBay 项目列表的开头或结尾不对应。 我用不同的 url 尝试了相同的代码,其中 ebay 列出了 >55 个项目,而 csv 到目前为止都出现了 55 个。 代码见附图。 任何帮助将不胜感激

从 bs4 导入 csv 导入 BeautifulSoup 导入请求

对于 soup.find_all('li', class_= 's-item') 中的项目:

try:  
    item_title = items.find('h3', class_='s-item__title').text
except Exception as e:
    item_title = 'None'
print(item_title)

try:
    item_price = items.find('span', class_='s-item__price').text
except Exception as e:
    item_price = 'None'
print(item_price)


#establishes list for writing to csv
var_list=[item_title,item_price +'\n']

#opens csv and writes list of product details
with open('history_phone_test.csv', 'a+', newline='') as products_file:
    products_writer = csv.writer(products_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    products_writer.writerow(var_list)
    

https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2334524.m570.l2632.R2.TR12.TRC2.A0.H0.Xiphone+8.TRS0&_nkw=iphone+8+plus&_sacat=9355&LH_TitleDesc= 0&_sop=16&_osacat=139973&_odkw=the+last+of+us+ps4+-elite+-limited+-figure+-theme+-pro+-edition+-art+-bundle+-rare+-promo+-console+-hand+-collector%27s+-remastered+-controllers+-statue+-无线+-控制器+-日文+-奖杯&rt=nc&_dcat=139973&Game%2520Name=%2520Last%2520of%2520Us%2520Part%2520II

当我访问该链接时,它每页只给我 50 个结果。 当您在浏览器上查看页面时,您可能按下了一些按钮来为每页提供更多结果,但我认为默认值(以及您的代码将使用的值)是 50。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM