代码拉取完成,页面将自动刷新
import requests
from lxml import etree
from bs4 import BeautifulSoup
def downloadHtml(url):
print(url)
header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
try:
res = requests.get(url,headers=header)
if res.status_code==200:
return res.text
else:
res.raise_for_status()
except:
print('downloadHtml:'+ url +" error!")
return 'ERROR'
def parserHtmlByCss(html):
ehtml = etree.HTML(html)
for i in ehtml.cssselect('.hd>a>span:nth-child(1)'):
print(i.text)
for i in ehtml.cssselect('.bd>p'):
print(i.text)
for i in ehtml.cssselect('.star>span.rating_num'):
print(i.text)
for i in ehtml.cssselect('.star>span:nth-child(4)'):
print(i.text)
for i in ehtml.cssselect('.quote>span'):
print(i.text)
for i in ehtml.cssselect('.pic>a>img'):
print(i.attrib['src'])
def parserHtmlByBs4(html):
soup = BeautifulSoup(html,'html.parser')
temp = soup.find_all(class_="pic")
for i in temp:
print(i.a.img['src'])
print(i.a.img['alt'])
temp = soup.find_all(class_="bd")
for i in temp:
typetype = i.p
print(str(typetype)[140:150])
temp = soup.find_all(class_="star")
for i in temp:
print(i.contents[3].string)
print(i.contents[7].string)
temp = soup.find_all(class_="inq")
for i in temp:
print(i.string)
def parserHtmlByXpath(html):
ehtml = etree.HTML(html)
xtitle = ehtml.xpath("//div[@class='hd']/a/span[1]/text()")
print(xtitle)
xtitle = ehtml.xpath("//div[@class='bd']/p[1]/text()[2]")
for i in xtitle:
print(i[41:50])
xtitle = ehtml.xpath("//div[@class='star']/span[2]/text()")
print(xtitle)
xtitle = ehtml.xpath("//div[@class='star']/span[4]/text()")
print(xtitle)
xtitle = ehtml.xpath("//span[@class='inq']/text()")
print(xtitle)
xtitle = ehtml.xpath("//div[@class='pic']/a//@src")
print(xtitle)
if __name__=='__main__':
for i in range(0,10):
url = 'https://movie.douban.com/top250?start='+ str(i*25)
print(url)
strhtml = downloadHtml(url)
movieInfo1 = parserHtmlByCss(strhtml)
movieInfo2 = parserHtmlByBs4(strhtml)
movieInfo3 = parserHtmlByXpath(strhtml)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。