python爬取淘宝信息
本人只是刚学python的菜鸟,代码不规范及需改进的地方请指教
我们直接看代码吧!哈哈
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
url = "https://list.tmall.com/search_product.htm?q=%CA%D6%BB%FA&type=p&vmarket=&spm=a211oj.0.a2227oh.d100&from=..pc_1_searchbutton"
res = requests.get(url=url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
xml = soup.find_all('p', class_="productTitle")
xml1 = soup.find_all('p', class_='productPrice')
xml2 = soup.find_all('a', class_='productShop-name')
for i in range(len(xml)):
name = xml[i].get_text()
price = xml1[i].get_text()
shop = xml2[i].get_text()
a = name.replace('\n', '')
b = price.replace('\n', '')
c = shop.replace('\n', '')
d = a + " " + b + " " + c + '\n'
with open('222.txt', 'a+', encoding='utf-8') as f:
f.write(d)
此段代码爬取的信息是淘宝搜索手机界面,关于商品名称、价格、店铺的信息。
小弟不才,我是一枚测试工程师,对python略感兴趣,所以学习了一下,不规范之处还请多指教,谢谢各位啦!!!!
爬取信息如上图所示
bs4库使用参考链接:bs4库的使用