马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
from bs4 import BeautifulSoup
start=0
result=[]
header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
for i in range (0,1):
html=requests.get('https://movie.douban.com/top250?start='+str(start)+'&filter=',headers=header)
html.encoding='utf-8'
start+=25
soup=BeautifulSoup(html.text,'html.parser')
for item in soup.find_all('div','info'):
title=item.div.a.span.string
yearline=item.find('div','bd').p.contents[2].string
yearline=yearline.replace('\n','')
yearline=yearline.replace(' ','')
year=yearline[0:4]
rating=item.find('span',{'class':'rating_num'}).get_text()
oneresult=[title,rating,year]
result.append(oneresult)
print(result)
上面是别人写的爬取豆瓣电影 Top 250(https://movie.douban.com/top250)的代码
我想要把它改成用来查询 豆瓣热门电视剧的标题和评分(https://movie.douban.com/tv/#!ty ... 20&page_start=0)
import requests
from bs4 import BeautifulSoup
start=0
result=[]
header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
for i in range (0,1):
html=requests.get('https://movie.douban.com/tv/#!type=tv&tag=%E6%97%A5%E5%89%A7&sort=time&page_limit=20&page_start='+str(start),headers=header)
html.encoding='utf-8'
start+=20
soup=BeautifulSoup(html.text,'html.parser')
for item in soup.find_all('div','list-wp'):
'不知道怎么写
oneresult=[title,rating,year]
result.append(oneresult)
print(result)
但是卡在这里不知道该怎么写
本帖最后由 1q23w31 于 2020-8-2 07:05 编辑 import requests
#from bs4 import BeautifulSoup #不需要此第三方库,由于不解析网页
'''页面生成受到js控制,故只加载页面得不出数据,改为直接解析数据'''
page_start=0
result=[]
header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
for j in range(1,3):
html = requests.get('https://movie.douban.com/j/search_subjects?type=tv&tag=%E6%97%A5%E5%89%A7&sort=time&page_limit=20&page_start='+str(page_start),headers=header)
data = html.json()
for i in range (0,20):
#html.encoding='utf-8'
#soup=BeautifulSoup(html.text,'html.parser')
#for item in soup.find_all('div','list-wp'):
#print(item)
#oneresult=[title,rating,year]
#result.append(oneresult)
#获取response数据
'''以上方法弃用'''
if data['subjects'][i]['rate'] == '':
data['subjects'][i]['rate'] = '暂无评分'
print(data['subjects'][i]['rate'],data['subjects'][i]['title'])
page_start+=20
#print(result)
代码已修改,看注释,不懂的再说
|