python运行没报错也没结果 10
importjsonimportrequestsfromlxmlimportetreedefpage(n):url=f"http://maoyan.com/board/4...
import jsonimport requestsfrom lxml import etreedef page(n): url = f"http://maoyan.com/board/4?off set={n*10}" header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6756.400 QQBrowser/10.3.2473.400"} r = requests.get(url, headers = header) return r.textdef prase(text): html = etree.HTML(text) names = html.xpath('//div[@class="movie-item-info"]/p[@class="name"]/a/@title') releasetimes = html.xpath('//p[@class="releasetime"]/text()') item = {} for name,releasetime in zip(names,releasetimes): item ['name'] = name item['releasetime'] = releasetime yield item def savefile(data): #保存数据 with open('movie.json','a',encoding='utf-8') as f: data = json.dumps(data,ensure_ascii=False) + '\n' f.write(data)def run(): for n in range(0,10): text = page(n) items = prase(text) for item in items: print(item) savefile(item)if __name__ == "_main_": run()
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |