from urllib.request import urlopen
from urllib.request import Request
import csv
from lxml import etree
fieldnames=['电影名称','电影主角','上映日期','电影网址']
con =open('URL爬虫.csv',mode='w',newline='',encoding='gbk')
whri = csv.DictWriter(con,fieldnames=fieldnames)
whri.writeheader()
for i in range(0,100,10):
url = f"https://www.maoyan.com/board/4?timeStamp=1698111309592&channelId=40011&index=5&signKey=644bc2337d5fc5ede0d94693fee42928&sVersion=1&webdriver=false&offset={i}"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.61',
'Host': 'www.maoyan.com',
'Referer': 'https://www.maoyan.com/',
'Cookie': '__mta=44511899.1698111302403.1698111310117.1698111 315087.3; uuid_n_v=v1; uuid=8C32E0F0720D11EEA7A245615871FFC5F3B52BCBD2854B05BDC3C691EBC3A74F; _csrf=f33774eec4730ec6994d1e563b01755f05044330e1c4e172ca7a9880a0129724; _lx_utm=utm_source%3Dbing%26utm_medium%3Dorganic; _lxsdk_cuid=18b5f5229c4c8-08795e0b67c8fa-745d5771-280000-18b5f5229c4c8; _lxsdk=8C32E0F0720D11EEA7A245615871FFC5F3B52BCBD2854B05BDC3C691EBC3A74F; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1698111302; __mta=44511899.1698111302403.1698111302403.1698111302403.1; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1698111315; _lxsdk_s=18b5f5229c4-6b6-333-243%7C%7C6'
}
def off(url):
http = Request(url,headers=headers)
foe = urlopen(http)
x = (foe.read().decode("utf-8"))
root = etree.HTML(x)
name = root.xpath('//p[@class="name"]/a[1]/text()')
daoyan = root.xpath('//p[@class="star"]/text()')
time = root.xpath('//p[@class="releasetime"]/text()')
htp = root.xpath('//p[@class="name"]/a/@href')
for i in range(0,10):
whri.writeheader=({'电影名称': name[i],'电影主角':daoyan[i],'上映日期':time[i],'电影网址':htp[i]})
off(url)
with open('URL爬虫.csv','a+',encoding='gbk',newline="")as f:
whri=csv.writer(f)
whri.writerow(fieldnames)
为什么写入csv的时候只写入了标题,内容没有写入