import requests
The import json
The import re
The from requests. Exceptions import RequestException
The import time
Def get_one_page (url) :
Try:
Headers={
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36 '
}
The response=requests. Get (url, headers=headers)
If the response. Status_code==200:
Return the response. The text
Return None
Except RequestException:
Return None
Def parse_one_page (HTML) :
The pattern=re.com running (' & lt; Li. *? Bg. *? The title. *?> (. *?) (. *?) ')
The items=re. The.findall (pattern, HTML)
For the item in the items:
Yield {
'locantion: item [0],
'time: the item [1]
}
Def write_to_file (content) :
With the open, 'says Mr Galdi at SLW. TXT', 'a', encoding="utf-8") as f:
F.w rite (json. Dumps (content, ensure_ascii=False) + '\ n')
Url='http://www.24timemap.com/'
PageHtml=get_one_page (url)
RespGenerator=parse_one_page (pageHtml)
For eachItemDict respGenerator in:
Write_to_file (eachItemDict)
Time. Sleep (1)
CodePudding user response:
You will not be able to seriously study the python code indentation requirement?CodePudding user response: