本学期python大作业的一部分hhh,贴完代码赶紧溜走肝作业污污污
import urllib.request
import urllib
import re
while True:
search=input('输入你想搜索的内容:')
search=urllib.parse.quote(search)
for i in range(10):
url='https://weixin.sogou.com/weixin?query='+str(search)+'&_sug_type_=&sut=4512&lkt=6%2C1543636830206%2C1543636833937&s_from=input&_sug_=y&type=2&sst0=1543636834039&ie=utf8&w=01019900&dr=1&ie=UTF-8'+'&page='+str(i)
headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
file=urllib.request.urlopen(url)
data=str(file.read().decode('utf-8'))
pat='target="_blank"(.*?) href="(http://.*?=1)"'
link=re.compile(pat).findall(data)
r=str(link).replace("amp;","")
pa='(http://.+?new=1)'
r2=re.compile(pa).findall(r)
r2=list(set(r2))
ptitle='data-share=".+?">(.+?)</a>'
r3=re.compile(ptitle).findall(data)
r3=str(r3).replace('<em><!--red_beg-->','')
r3=r3.replace('<!--red_end--></em>','')
r3=r3.replace('“','')
r3=r3.replace('”','')
r3=r3.replace('[','')
r3=r3.replace(']','')
r3=r3.split("', '")
for m,n in zip(r2,r3):
print(m,n)