【问题标题】:Python 3.7.6 web scraper writing to csv truncates resultsPython 3.7.6 网络爬虫写入 csv 会截断结果
【发布时间】:2020-10-14 02:48:14
【问题描述】:

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

导入 csv 从 bs4 导入 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+-wireless+-controller+-japanese+-trophy&rt=nc&_dcat=139973&Game%2520Name=The%2520Last%2520of%2520Us%2520Part%2520II

【问题讨论】:

    标签: python web-scraping export-to-csv


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复!我重新检查了链接,仍然得到 +2k 列表。此外,我的 csv 获得 55 行数据而不是 50 行,因此 eBay 默认值 50 似乎不适用于此处。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2013-10-15
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多