爬取豆瓣电影信息
import requestsimport bs4
import time
with open('./豆瓣.txt',mode='w+',encoding='utf-8') as f:
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
q = input("输入要爬几页")
for i in range(int(q)):
url = "https://movie.douban.com/top250"
params = {q : (i * 25)}
res = requests.get(url=url, headers=headers, params=params)
soup = bs4.BeautifulSoup(res.text, "html.parser")
targets = soup.find_all("div", class_="hd")
for each in targets:
f.write(each.a.text)
time.sleep(1)
为什么无法爬取下一页,一直在第一页里循环 import requests
import bs4
import time
with open('./豆瓣.txt',mode='w+',encoding='utf-8') as f:
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
q = input("输入要爬几页")
for i in range(int(q)):
url = f"https://movie.douban.com/top250?start={i * 25}&filter="
res = requests.get(url=url, headers=headers)
soup = bs4.BeautifulSoup(res.text, "html.parser")
targets = soup.find_all("div", class_="hd")
for each in targets:
f.write(each.a.text)
time.sleep(1) 你一直请求第一页当然不变 一直是第1页{:10_257:} 鱼币 qiuyouzhi 发表于 2020-8-7 22:01
现在没用了吗?
页:
[1]