python爬取京东手机数据

import requests
from bs4 import BeautifulSoup(导入库)
url= ’ https://search.jd.com/Search?keyword=手机&enc=utf-8&wq=手机&pvid=52e64196617245969a2a0d3fc2110475’
headers ={‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36’,}
ret = requests.get(url,headers=headers)
ret = ret.content.decode()
soup = BeautifulSoup(ret,‘lxml’)
soup_mingzi = soup.find_all(‘div’,class_=“p-name p-name-type-2”)
souo_jiage = soup.find_all(‘div’,class_=“p-price”)
#print(souo_jiage)
for u in souo_jiage:
u = u.text

for i in soup_mingzi:
i=i.text
string = ‘

%s

%s’ % (i, u)
with open(‘京东手机.html’, ‘a’, encoding=‘utf-8’)as fp:
fp.write(string)

你可能感兴趣的:(python学习)