import requests,csv
headers={"User-Agent": '''Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'''}
url1="https://www.jianshu.com/asimov/books?page="
url2="&count=12&category_id=284"
infs=[]
for i in range(5):
geturl=url1+str(i*1)+url2
reponse=requests.get(geturl,headers=headers)
strs = reponse.json()
films=strs["books"]
for film in films:
title=film["name"]
score=film["categories"]
infs.append([title,score])
with open ("D://电影信息3.csv","w+",encoding="utf-8",newline="") as f:
writer=csv.writer(f)
writer.writerows(infs)
print(geturl)
from bs4 import BeautifulSoup
import requests,csv
url = 'https://movie.douban.com/review/best/'
headers={"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36" }
r = requests.get(url,headers = headers).text
soup = BeautifulSoup(r,'lxml')
divs = soup.findAll('div',{'class':'main review-item'})
rows = []
for div in divs:
head = div.find('h2').text
body = div.find('div',{'class':'short-content'}).text
rows.append([head,body])
with open('spiderdemo3.csv','w+',encoding='utf-8',newline='') as f:
w = csv.writer(f)
w.writerow(['head','body'])
w.writerows(rows)