1,
安裝requests 和 beautifulsoup
2,安裝lxml
https://pypi.pyth——on.org/pypi/ez_setup
python ez_setup.py
https://pypi.python.org/pypi/pip
python setup.py install
3,
import sys
reload(sys)
sys.setdefaultencoding("utf-8" )
import requests
from bs4 import BeautifulSoup
def getHtmlText(url):
try:
r=requests.get(url)
r.raise_for_status()
r.encoding= r.apparent_encoding
print('success')
return r.text
except:
print('false')
return 'false'
def parseHtml(finddatas, html):
soup= BeautifulSoup(html, 'lxml')
lis= soup.find_all('li', class_="gl-item")
print(len(lis))
for i in range(len(lis)):
try:
title= lis[i].a['title']
price= lis[i].find('div', class_='p-price').i.string
finddatas.append([title, price])
except:
print('')
def displayHtmlGoods(finddatas):
std="{:6} {:8} {:16}
"
f= open('out.txt','a')
f.write(std.format(u'序號(hào)', u'價(jià)格', u'商品名稱(chēng)'))
for i in range(len(finddatas)):
f.write(std.format(i+1, finddatas[i][1], finddatas[i][0]))
print('out ok')
f.close()
def main():
url_basic= 'https://search.jd.com/Search?keyword='
total_pages= 3
keyword= u'電腦'
finddatas= []
for i in range(total_pages):
page= 1 + i * 2
url= url_basic + keyword + '&enc=utf-8&wq=' + keyword + '&page=' + str(page)
print(url)
html= getHtmlText(url)
parseHtml(finddatas, html)
displayHtmlGoods(finddatas)
if __name__ == '__main__':
main()
網(wǎng)站名稱(chēng):Python_1_requests-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article10/djsodo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、Google、動(dòng)態(tài)網(wǎng)站、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容