python 爬虫实现
本文使用python3 实现从谷歌学术获得搜索结果
模拟浏览器发送请求
网络访问的模型使用请求应答的模型。客户端发送请求,浏览器相应请求。
使用chrome浏览器获得请求方式
在f12开发者模式下,查看请求头,发现是使用get方法。复制为url得到请求内容
为了模拟浏览器,所以使用headers。
在headers中可以将cookies删除,测试不影响
在python中实现
使用rullib中的模块
数据分析
使用正则表达式
分析html文件。通过正则表达式匹配
代码块
import urllib.parse
import urllib.request
import re
keyword=input("keywords is?\n")
print(keyword)
url='https://scholar.google.com/scholar?&hl=en&q='+keyword+'&btnG=&lr='
header_dict={
'Host': 'scholar.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Referer': 'https://scholar.google.com/schhp?hl=zh-CN',
'Connection': 'keep-alive'}
req = urllib.request.Request(url=url,headers=header_dict)
response = urllib.request.urlopen(req,timeout=120)
#print(f.read())
#with open('aaa.html', 'wb') as f:
# f.write(response.read())
print("conneect succeed!")
'''data=response.read().decode('utf-8')
pattern = re.compile(r'<div class="gs_r"><div class="gs_ri"><h3.*?<a onclick',re.S)
for m in re.finditer(pattern,data):
print (m.group())
'''
#print(response.read())
data=response.read()
data=data.decode()
pattern = re.compile(r