我在用python beautifulsou,requests,Pandas library爬行网页, 试图通过for循环收集多个页面中多个项目的信息。 但当我运行这些代码时, 我只能将列表彼此分开,所以我想编辑此代码,使其由一个列表串联起来。你知道吗
Windows、Jupyter笔记本、Python
def a(content):
ptag_title=content.find("p",{"class":"title"})
ptag_price=content.find("p",{"class":"price-sale"})
return {"title":ptag_title.text, "price":ptag_price.text}
def get_pd_page(url):
result = requests.get(url)
bs_obj = bs4.BeautifulSoup(result.content,"html.parser")
pbl=bs_obj.find("div",{"class":"product-box-list"})
contents = pbl.findAll("div",{"class":"content"})
pdinfo_list = [get_pdinfo(content ) for content in contents]
return pdinfo_listn = 10
urls = [None] * n
fix_str = "https://www.abcdef.com"
for page_num in range(0,n):
page_str = fix_str + str(page_num+1)
urls[page_num] = page_str
page_products = get_pd_page(urls[page_num])
print(page_products)
每个页面的结果都是分开的列表。你知道吗
[{'title':a, 'price'=b},{'title':c, 'price'=d}] [{'title':d, 'price'=e},{'title':f, 'price'=g]
我想列一整张单子。你知道吗
[{'title':a, 'price'=b},{'title':c, 'price'=d},{'title':d, 'price'=e},{'title':f, 'price'=g]
或者,至少,通过列表列表
[[{'title':a, 'price'=b},{'title':c, 'price'=d}],[{'title':d, 'price'=e},{'title':f, 'price'=g]]
使用
+
操作符连接任意数量的列表或者使用列表理解来连接列表列表(也称为
flattening
列表)中的子列表这些都是简单的例子,然后你试图实现,但一个类似的方法将解决你的问题在最后!你知道吗
相关问题 更多 >
编程相关推荐