Home >
other > (for help, want to climb in the world of time, here is the source code, thank you)
(for help, want to climb in the world of time, here is the source code, thank you)
The import json
The import requests
The from requests. Exceptions import RequestException
The import re
The import time def get_one_page (url) : try: headers={' the user-agent ':' Mozilla/5.0 (Windows NT 6.1; Win64. X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 '} response=requests. Get (' http://www.24timemap.com/'headers=headers). If the response status_code==200: return response. The text return None except RequestException: return None
Def parse_one_page (HTML) : the pattern=re.com running (' & lt; Div. *?> (. *?)
. *? (a/d +). *?
') the items=re. The.findall (pattern, HTML) for the item in the items: yield {' location: item [0], 'time: the item [1]} print (item) write_to_file (item)
Def write_to_file (content) : with the open (' r1. TXT ', 'a', encoding="utf-8") as f: f.w rite (json. Dumps (content, ensure_ascii=False) + '\ n') time. Sleep (1)
CodePudding user response:
The pattern=re.com running (' & lt; Div. *?> (. *?)
. *? (a/d +). *?