37,740
社区成员
发帖
与我相关
我的任务
分享
import requests
import json
import re
import time
from requests.exceptions import RequestException
url ='http://www.24timemap.com/'
def get_one_page(url):
try:
headers={
'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'
}
response = requests.get(url,headers=headers)
if response.status_code ==200:
return response.text
return None
except RequestException:
return None
def parse_one_page(html):
pattern = re.compile('<li.*?bg.*?title.*?>(.*?)</a>(.*?)</li>',re.S)
items = re.findall(pattern,html)
for item in items:
yield {
'locantion': item[0],
'time': item[1]
}
def write_to_file(content):
with open('slw.txt', 'a', encoding='utf-8')as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n')
def write_to_file(content):
with open('slw.txt', 'a', encoding='utf-8')as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n')
def write_to_file(content):
with open('slw.txt', 'a', encoding='utf-8')as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n')
pageHtml = get_one_page(url)
respGenerator = parse_one_page(pageHtml)
for eachItemDict in respGenerator:
write_to_file(eachItemDict)