【问题标题】:how to scrape deeply embeded links with python beautifulSoup如何使用 python beautifulSoup 抓取深层嵌入的链接
【发布时间】:2015-03-08 09:59:02
【问题描述】:

我正在尝试构建一个用于学术目的的蜘蛛/网络爬虫,以从学术出版物中获取文本并将相关链接附加到 URL 堆栈。我正在尝试抓取 1 个名为“PubMed”的网站。我似乎无法获取我需要的链接。这是我的代码示例页面,这个页面应该代表他们数据库中的其他页面:

 website = 'http://www.ncbi.nlm.nih.gov/pubmed/?term=mtap+prmt'
 from bs4 import BeautifulSoup
 import requests
 r = requests.get(website)
 soup = BeautifulSoup(r.content)

为了便于阅读,我将 html 树分解为几个变量,以便它们都适合 1 个屏幕宽度。

 key_text = soup.find('div', {'class':'grid'}).find('div',{'class':'col twelve_col nomargin shadow'}).find('form',{'id':'EntrezForm'})
 side_column = key_text.find('div', {'xmlns:xi':'http://www.w3.org/2001/XInclude'}).find('div', {'class':'supplemental col three_col last'})
 side_links = side_column.find('div').findAll('div')[1].find('div', {'id':'disc_col'}).findAll('div')[1]

 for link in side_links:
      print link

如果您使用 chrome inspect 元素查看 html 源代码,则应该有几个其他嵌套的 div 带有在“side_links”中的链接。但是上面的代码会产生以下错误:

 Traceback (most recent call last):
 File "C:/Users/ballbag/Copy/web_scraping/google_search.py", line 22, in <module>
 side_links = side_column.find('div').findAll('div')[1].find('div',      {'id':'disc_col'}).findAll('div')[1]
 IndexError: list index out of range

如果您转到网址,右侧有一列名为“相关链接”的列,其中包含我希望抓取的网址。但我似乎无法接近他们。在我试图进入的 div 下有一个声明,我怀疑这与它有关。任何人都可以帮助获取这些链接吗?我真的很感激任何指针

【问题讨论】:

  • 我在该页面上没有看到任何related links
  • 抱歉 'pubmed 中的相关引用'

标签: python html web-scraping beautifulsoup html-parsing


【解决方案1】:

问题是边栏加载了一个额外的异步请求。

这里的想法是:

  • 使用requests.Session 维护网络抓取会话
  • 解析获取侧边栏的url
  • 关注该链接并从div 和class="portlet_content" 获取链接

代码:

from urlparse import urljoin

from bs4 import BeautifulSoup
import requests


base_url = 'http://www.ncbi.nlm.nih.gov'
website = 'http://www.ncbi.nlm.nih.gov/pubmed/?term=mtap+prmt'

# parse the main page and grab the link to the side bar
session = requests.Session()
soup = BeautifulSoup(session.get(website).content)

url = urljoin(base_url, soup.select('div#disc_col a.disc_col_ph')[0]['href'])

# parsing the side bar
soup = BeautifulSoup(session.get(url).content)

for a in soup.select('div.portlet_content ul li.brieflinkpopper a'):
    print a.text, urljoin(base_url, a.get('href'))

打印:

The metabolite 5'-methylthioadenosine signals through the adenosine receptor A2B in melanoma. http://www.ncbi.nlm.nih.gov/pubmed/25087184
Down-regulation of methylthioadenosine phosphorylase (MTAP) induces progression of hepatocellular carcinoma via accumulation of 5'-deoxy-5'-methylthioadenosine (MTA). http://www.ncbi.nlm.nih.gov/pubmed/21356366
Quantitative analysis of 5'-deoxy-5'-methylthioadenosine in melanoma cells by liquid chromatography-stable isotope ratio tandem mass spectrometry. http://www.ncbi.nlm.nih.gov/pubmed/18996776
...
Cited in PMC http://www.ncbi.nlm.nih.gov/pmc/articles/pmid/23265702/citedby/?tool=pubmed

【讨论】:

  • 我什至找不到 OP 正在寻找的东西!
  • @PadraicCunningham 打开你的想象力或开始通过互联网阅读想法! :) 谢谢。
  • 谢谢,你的密码很完美。只是为了我的理解,你在哪里找到了行中的'href':“url = urljoin(base_url,soup.select('div#disc_col a.disc_col_ph')[0] ['href'])”。我可以导航到 div 但“chromes 检查元素”在 html 的该部分中没有显示任何链接。尽管您的代码有效,但我在 div#disc_col 中看不到“a”链接。我也无法在基本网址中找到相关部分。如果您能推荐任何文章来帮助我理解,那就太好了。否则,无论如何,谢谢。
  • @user3062260 这是您在浏览器中找不到的,因为浏览器可以修改最初检索到的 HTML,应用从其他异步请求接收到的内容,或执行 javascript 代码。虽然您使用requests.get() 或session.get() 得到的是初始HTML 页面 - 您可以在此处找到链接div#disc_col a.disc_col_ph。换句话说,打印出session.get(website).content 并查找链接。希望能把事情弄清楚一点。
  • @user3062260 好吧,您也可以在浏览器中看到初始 HTML - 打开浏览器开发人员工具、网络选项卡并检查第一个请求的响应。
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 2021-08-17
  • 2014-06-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
相关资源
最近更新 更多