相对于上一篇爬百度学术的,这篇爬的是ScienceDirect,英文版的,这里面提供的内容更全,有标题,完整摘要,作者,论文相关信息等
注意需要科学上网才能爬取
import time
import requests
from bs4 import BeautifulSoup
from urllib.request import quote
search = input('请输入关键词:')
kwen = search.encode('utf-8') #将汉字,用utf格式编码,赋值给gbkkw
f = open('Sciencedirect.txt', 'w', encoding ='utf-8') #创建txt格式文件,方便等会存储
#添加请求头,模拟浏览器正常访问,避免被反爬虫
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
for x in range(4): #想要爬取多少页数据
#time.sleep(10)
#https://ieeexplore.ieee.org/search/searchresult.jsp?newsearch=true&queryText=TCM%20treatment&highlight=true&returnType=SEARCH&matchPubs=true&pageNumber=1&returnFacets=ALL
#https: // www.sciencedirect.com / search?qs = TCM & offset = 50
url = 'https://www.sciencedirect.com/search?qs='+quote(kwen)+'&offset='+str(x*10)
res = requests.get(url,headers = headers)
print(res.status_code) #查看是否能获取数据
bs1 = BeautifulSoup(res.text, 'html.parser') #解析数据
list_titles = bs1.find_all('div'