爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新
import requests from requests.exceptions import RequestException import re import json def get_one_page(url): try: response = requests.get(url) if response.status_code == 200:#判断网页是否正常开启 return response.text#返回没有 return None except RequestException: return None def parse_one_page(html): pattern = re.compile("", re.S)#笔记本屏幕太小看不过来 items = re.findall(pattern, html) for item in items:#把爬取的信息进行格式化 yield{ "":item[0], "":item[1], } def write_to_file(content): with open("result.txt", "a", encoding="utf-8") as f: f.write(json.dumps(content, ensure_ascii=False) + " ") f.close() def main(): url = "https://movie.douban.com/top250" html = get_one_page(url) # parse_one_page(html) for item in parse_one_page(html): print(item) write_to_file(item) if __name__ == "__main__": main() #这段代码没有加RE的匹配方式(笔记本屏幕小) #第二个这段代码只能匹配一页的东西,自己发挥想象把