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')
uj5u.com熱心網友回復:
還沒看明白網頁的,不過建議你用代碼插入一下,這看著太亂了uj5u.com熱心網友回復:
建議你重發,代碼插入轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/78132.html
上一篇:python安裝pyinstaller失敗,不懂得什么原因,跪求大佬指導
下一篇:Python爬蟲網頁亂碼求解
