Python Webscraping-如何附加列
我正在抓取多个GoogleScholar页面,我已经用BeautifulSoup编写了代码来提取标题、作者、期刊等信息 这是一个示例页面。 现在我想提取有关h-index、I-10索引和引文的信息。当我查看页面时,我看到所有这些都有相同的类名(gsc_rsb_std)。鉴于此,我的怀疑是Python Webscraping-如何附加列,python,web-scraping,xpath,beautifulsoup,append,Python,Web Scraping,Xpath,Beautifulsoup,Append,我正在抓取多个GoogleScholar页面,我已经用BeautifulSoup编写了代码来提取标题、作者、期刊等信息 这是一个示例页面。 现在我想提取有关h-index、I-10索引和引文的信息。当我查看页面时,我看到所有这些都有相同的类名(gsc_rsb_std)。鉴于此,我的怀疑是 如何使用beautiful soup提取此信息?下面的代码提取了有关引用的信息。由于类名相同,如何为其他两个执行此操作 名称、引用、h索引和i索引只有一个值。但是,有多行文件。理想情况下,我希望我的输出为以下
soup = BeautifulSoup(driver.page_source, 'html.parser')
columns = {}
columns['Name'] = soup.find('div', {'id': 'gsc_prf_in'}).text
papers = soup.find_all('tr', {'class': 'gsc_a_tr'})
for paper in papers:
columns['title'] = paper.find('a', {'class': 'gsc_a_at'}).text
File.append(columns)
我的输出是这样的。看起来这个循环有点问题
Name h-index paper3
Name h-index paper3
Name h-index paper3
谢谢你的帮助。提前谢谢 > P>我会考虑使用:包含和包含和目标搜索字符串< /P>
import requests
from bs4 import BeautifulSoup
searches = ['Citations', 'h-index', 'i10-index']
r = requests.get('https://scholar.google.com/citations?user=cp-8uaAAAAAJ&hl=en')
soup = BeautifulSoup(r.text, 'html.parser')
for search in searches:
all_value = soup.select_one(f'td:has(a:contains("{search}")) + td')
print(f'{search} All:', all_value.text)
since_2016 = all_value.find_next('td')
print(f'{search} since 2016:', since_2016.text)
您还可以使用pandasread_html
按索引获取该表
问题: 元素有一个id,使用css选择器/按id查找元素可以更快地匹配,例如
driver.find_element_by_id("gsc_prf_in").text
P.>我不需要,但是,当刮取这个页面时,硒是不需要的。 < P>我会考虑使用:有和:包含和目标用搜索字符串< /P>
import requests
from bs4 import BeautifulSoup
searches = ['Citations', 'h-index', 'i10-index']
r = requests.get('https://scholar.google.com/citations?user=cp-8uaAAAAAJ&hl=en')
soup = BeautifulSoup(r.text, 'html.parser')
for search in searches:
all_value = soup.select_one(f'td:has(a:contains("{search}")) + td')
print(f'{search} All:', all_value.text)
since_2016 = all_value.find_next('td')
print(f'{search} since 2016:', since_2016.text)
您还可以使用pandasread_html
按索引获取该表
问题: 元素有一个id,使用css选择器/按id查找元素可以更快地匹配,例如
driver.find_element_by_id("gsc_prf_in").text
不过,我认为在抓取此页面时没有必要使用selenium。您可以使用Chrome extension直观地抓取CSS选择器。下面是一些快速的例子和解释
突出显示在以下内容中的元素:
- 红色从搜索中排除
- 绿色包含在搜索中
- 黄色是指猜测用户要查找的内容,需要进一步澄清
get_author_info.py
->在main.py
中取消注释以进行测试)中的代码和示例:
从bs4导入美化组
导入请求,lxml,操作系统
标题={
“用户代理”:
“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582”
}
代理={
'http':os.getenv('http\u PROXY')
}
html=requests.get('https://scholar.google.com/citations?user=cp-8UAAAAJ&hl=en',标题=标题,代理=代理)。文本
soup=BeautifulSoup(html,“lxml”)
对于soup中被引用的公共访问。选择('.gsc_rsb'):
引文\全部=由\公共\访问引用\选择一个('tr:nth child(1).gsc\ u rsb\ u sc1+.gsc\ u rsb\ u std')。文本
引文自2016年起=被公共访问引用。选择一个('tr:nth child(1).gsc\u rsb\u std+.gsc\u rsb\u std')。文本
h_index_all=被公共访问引用。选择其中一个('tr:nth child(2).gsc_rsb_sc1+.gsc_rsb_std')。文本
h_index_2016=被公共访问引用。选择其中一个('tr:nth child(2).gsc_rsb_std+.gsc_rsb_std')。文本
i10_index_all=公共访问引用。选择一个('tr~tr+tr.gsc_rsb_sc1+.gsc_rsb_std')。文本
i10_index_2016=公共访问引用。选择一个('tr~tr+tr.gsc_rsb_std+.gsc_rsb_std')。文本
articles_num=被公共访问引用。选择一个('.gsc_rsb_m_a:n个子(1)span')。text.split('')[0]
文章链接=被公共访问引用。选择一个('gsc\U lwp\U mndt\U lnk')['href']
打印('城市化信息:')
打印(f{引文{所有引文}\n{自2016年以来引文}\n{h{U索引}\n{h{U索引}2016}\n{i10索引}\n所有引文}\n{i10索引}\n}\nhttps://scholar.google.com{文章链接}\n')
输出:
城市化信息:
55399
34899
69
59
148
101
23
https://scholar.google.com/citations?view_op=list_mandates&hl=en&user=cp-8uaaaaj
或者,您也可以使用SerpApi做同样的事情。这是一个付费API,免费试用5000次搜索 在一个特定的例子中,主要的区别在于,您不必猜测和修补如何获取HTML页面的某些元素 另一件事是,您不必考虑如何解决CAPTHCA(它可能会出现在某个点上),或者在需要许多请求的情况下找到好的代理 要集成的代码:
从serpapi导入谷歌搜索
导入操作系统
参数={
“api_键”:os.getenv(“api_键”),
“引擎”:“谷歌学者”,
“作者id”:“cp-8UAAAAJ”,
“hl”:“en”,
}
搜索=谷歌搜索(参数)
结果=search.get_dict()
引文全部=结果['QUICTED_']['table'][0]['QUICTES']['all']
引文数量=结果[“引文数量”][“表”][0][“引文数量”][“自2016年以来”]
h_inedx_all=结果['QUICED_by']['table'][1]['h_inedx']['all']
2016年h_指数=结果['被引用]['表格][1]['h_指数]['自2016年以来]]
i10_index_all=结果['QUICED_by']['table'][2]['i10_index']['all']
i10指数2016=自2016年以来的结果[“被引用”][“表”][2][“i10指数”][“自2016年以来”]
打印(f'{引文{引文}\n{引文{2016}\n{h_inedx{U all}\n{h_索引{2016}\n{i10_索引}\n{i10_索引}\n')
public\u access\u link=结果['public\u access']['link']
public\u access\u available\u articles=结果['public\u access']['available']
打印(f{public\u access\u link}\n{public\u access\u available\u articles})
输出:
55399
34899
69
59
148
101
https://scholar.google.com/citations?view_op=list_mandates&hl=en&user=cp-8uaaaaj
23
免责声明,我为SerpApi工作
您可以使用Chrome扩展来直观地获取CSS选择器。下面是一些快速的例子和解释
突出显示在以下内容中的元素:
- 红色从搜索中排除
- 绿色包含在搜索中
- 黄色是指猜测用户要查找的内容以及需要添加的内容