豆瓣爬虫求助!
import requestsfrom 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.string
yearline=yearline.replace('\n','')
yearline=yearline.replace(' ','')
year=yearline
rating=item.find('span',{'class':'rating_num'}).get_text()
oneresult=
result.append(oneresult)
print(result)
上面是别人写的爬取豆瓣电影 Top 250(https://movie.douban.com/top250)的代码
我想要把它改成用来查询 豆瓣热门电视剧的标题和评分(https://movie.douban.com/tv/#!type=tv&tag=%E6%97%A5%E5%89%A7&sort=time&page_limit=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=
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=
#result.append(oneresult)
#获取response数据
'''以上方法弃用'''
if data['subjects']['rate'] == '':
data['subjects']['rate'] = '暂无评分'
print(data['subjects']['rate'],data['subjects']['title'])
page_start+=20
#print(result)
代码已修改,看注释,不懂的再说 1q23w31 发表于 2020-8-2 06:59
代码已修改,看注释,不懂的再说
谢谢你,原来用html读不到数据
要用json来获取数据 ashlike 发表于 2020-8-2 15:32
谢谢你,原来用html读不到数据
要用json来获取数据
https://movie.douban.com/j/search_subjectstype=tv&tag=%E6%97%A5%E5%89%A7&sort=time&page_limit=20&page_start=0
这个网址不是网页的地址,而是数据的地址,不是用json读取的问题
页:
[1]