https://blog.csdn.net/qq_40099293/article/details/112587413
上一篇我们已经可以得到这样的表格
那么这里就可以通过xlsxwriter 固定读取 第四列数据,table.nrows是最大行防止取空报错
def read_excel(row):
# 打开文件
data = xlrd.open_workbook('E:/耳机商品列表—英文 - 副本.xlsx')
table = data.sheet_by_name('Sheet1')
# print(table.name, table.nrows, table.ncols)
if row < table.nrows:
return table.cell(row, 3).value
else:
return 0
然后不断,行加一从而循环获取商品详情页的网址了,我们直接吧行设置为调用参数
本片完全可以在上一个程序的基础上修改一部分即可完成功能,不过亚马逊的网页让人很不舒服,
1:有多个相同的class /ID
2: Product information 不同商品还不一样
3:网页加载太慢,而且容易卡死。
针对问题,我们还是可以不断细分
from pyquery import PyQuery as Pq
doc = Pq(page_source)
doc就是整个页面的源代码,和你在谷歌浏览器右键查看源代码一样。然后就可以直接用里面的方法doc(’#id名.class名’).text=() 通过id和类名确定一段字符串取出文本信息,如果要取id名或者class名
doc.attr(‘id名字’) 当然你也可以doc(’.a-link-normal.a-text-normal’).attr(‘href’) 无限组合。
了解以上规则后取文本就简单多了
title = main_list(’.a-size-large.product-title-word-break’).text() 取标题
取价格的时候很头痛 亚马逊的价格样式有很多种设计一个简单的函数来判断确保有价格抓入。
price = ""#该死的作用域
j = 0
while 1:
if len(price) == 0 and j == 0:
price = main_list("[class='a-size-medium a-color-price priceBlockBuyingPriceString']").text()
j = j + 1
elif len(price) == 0 and j == 2: