Python_1_requests-创新互联

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'序号', u'价格', u'商品名称'))

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()


文章标题:Python_1_requests-创新互联
转载来于:http://scyanting.com/article/djsodo.html