python requests模塊演示
import requests
from bs4 import BeautifulSoup
import csv
import re
import time
import json
import os.path
import random
record ={}
def func(date):
? ? global writer
? ? dailylist =[date]
? ? record[date]={}
? ? url='https://weixin.sogou.com/weixin?type=2&s_from=input&query={}+各區(qū)確診病例++shanghaifabu&ie=utf8&_sug_=y&_sug_type_=&w=01019900&sut=523&sst0=1652065715152&lkt=0%2C0%2C0'\
? ? ? ? ?.format(date)
? ? headers={? ? 'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) \
? ? ? ? ? ? appleWebKit/537.36 (KHTML, like Gecko) Chrome/\
? ? ? ? ? ? 86.0.4240.75 Safari/537.36',
}
? ? sogoulink = 'https://weixin.sogou.com'
? ? data=requests.get(url,headers = headers,timeout=5)
? ? cookie = data.cookies.get_dict()
? ? headers['cookie']=\
''.join([str(item[0])+'='+str(item[1])+'; ' for item in cookie.items()])
? ? soup = BeautifulSoup(data.text,features = 'html.parser')
? ? find =False
? ? for n in range(8):
? ? ? ? account = (soup.select('#sogou_vr_11002601_account_{}'.format(n)))[0].text
? ? ? ? if account =='上海發(fā)布':
? ? ? ? ? ? title = (soup.select('#sogou_vr_11002601_title_{}'.format(n)))[0]
? ? ? ? ? ? titletext = title.text
? ? ? ? ? ? if date in titletext and '本市各區(qū)' in titletext \
? ? ? ? ? ? ? ?and '確診病例' in titletext and '居住地信息'in titletext:
? ? ? ? ? ? ? ? link = sogoulink + title.get('href')
? ? ? ? ? ? ? ? find = True
? ? ? ? ? ? ? ? print(titletext)
? ? ? ? ? ? ? ? break
? ? time.sleep(0.5)
? ? if find:
? ? ? ? daily = requests.get(link,headers = headers,timeout =5)
? ? ? ? a=re.findall(r"url \+.*'",daily.text)
? ? ? ? flink = ''.join([t.replace('url += ','').replace("\'",'') for t in a])
? ? ? ? time.sleep(0.5)
? ? ? ? fdaily = requests.get(flink,headers = headers,timeout =5)
? ? ? ? chips = re.findall(r'{}.*?p>'.format(date),fdaily.text)
? ? ? ? chipsp = re.sub('[,、]',',',chips[0]).split(',')
? ? ? ? for item in chipsp:
? ? ? ? ? ? if '確診' in item and '無(wú)癥狀'in item and '隔離'not in item:
? ? ? ? ? ? ? ? totquezhen =int(re.findall('\d+',item)[0])
? ? ? ? ? ? ? ? totwuzheng =int(re.findall('\d+',item)[1])
? ? ? ? ? ? ? ? record[date]['total']=[int(totquezhen),int(totwuzheng)]
? ? ? ? ? ? ? ? dailylist.append(totquezhen+totwuzheng)
? ? ? ? ? ? ? ? print('總計(jì)確診:{} 無(wú)癥狀:{}'.format(totquezhen,totwuzheng))
? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ??
? ? ? ? for chip in chips[1:]:
? ? ? ? ? ? chipsp = re.sub('[,、]',',',chip)# # 中文標(biāo)點(diǎn)轉(zhuǎn)化為英文逗號(hào)
? ? ? ? ? ? qu = (re.findall(',.+區(qū)',chipsp))[0][1:]
? ? ? ? ? ? qu = re.sub(r'<.*>','',qu)
? ? ? ? ? ??
? ? ? ? ? ? quezhen =0
? ? ? ? ? ? wuzheng =0
? ? ? ? ? ? chipsp = chipsp.split(',')
? ? ? ? ? ? for item in chipsp:
? ? ? ? ? ? ? ? if '確診' in item and re.findall('\d+',item) and '無(wú)癥狀' not in item:
? ? ? ? ? ? ? ? ? ? quezhen = int(re.findall('\d+',item)[0])
? ? ? ? ? ? ? ? if '無(wú)癥狀' in item and re.findall('\d+',item) and '確診'not in item:
? ? ? ? ? ? ? ? ? ? wuzheng = int(re.findall('\d+',item)[0])
? ? ? ? ? ? print('{} 確診:{} 無(wú)癥狀:{}'.format(qu,quezhen,wuzheng))
? ? ? ? ? ? record[date][qu]=[quezhen,wuzheng]
? ? ? ? ? ? dailylist.append(quezhen+wuzheng)
? ? ? ? if sum([ int(record[date][f][0]) for f in record[date].keys() if f!='total'])\
? ? ? ? ? ?!= int(totquezhen) or\
? ? ? ? ? ?sum([ int(record[date][f][1]) for f in record[date].keys() if f!='total'])\
? ? ? ? ? ?!=? int(totwuzheng):
? ? ? ? ? ? print('統(tǒng)計(jì)存在偏差!')
? ? else:
? ? ? ? print('未找到數(shù)據(jù)!')
? ? ? ??
for day in range(12,13):
? ? date ='5月{}日'.format(day)
? ? try:
? ? ? ? func(date)
? ? except:
? ? ? ? print(date,' error')
? ? time.sleep(5)