【问题标题】:Extract data from dynamic HTML Table with Python 3使用 Python 3 从动态 HTML 表中提取数据
【发布时间】:2020-10-17 02:33:18
【问题描述】:

我一直在编写一个 python 3 脚本来生成 BibTeX 条目,并拥有ISSN's,我想用它来获取有关相关期刊的信息。

比如我想取ISSN0897-4756,发现这是Chemistry of Materials期刊,由ACS Publications出版。

我可以使用this site 手动执行此操作,我要查找的信息存储在lxml 表//table[@id="journal-search-results-table"] 中,或者更具体地说,存储在其表体的单元格中。

但是,我无法使用 python 3.x 成功实现自动化

我曾尝试使用httplib2requestsurllib2lxml.html 包中的方法访问数据,但迄今为止没有成功。

我目前所拥有的如下所示:

import certifi       
import lxml.html
import urllib.request

ISSN = "0897-4756"
address = "https://www.journalguide.com/journals/search?type=journal-name&journal-name={}".format(ISSN)

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  'Accept-Encoding': 'none',
  'Accept-Language': 'en-US,en;q=0.8',
  'Connection': 'keep-alive'}

request = urllib.request.Request(address,None,hdr)  #The assembled request
response = urllib.request.urlopen(request)
html = response.read()
tree = lxml.html.fromstring(html)

print(tree.xpath('//table[@id="journal-search-results-table"]/text()'))
# >> ['\n', '\n']
# Shows that I am connecting to the table

print(tree.xpath('//table[@id="journal-search-results-table"]//td/text()'))
# >> []
# Should???? hold the data segments that I am looking for?

Exact page being queryed by the above

据我所知,似乎表格的 tbody 元素,因此它包含的 trtd 元素在 python 解释 HTML 时没有被加载 - 这是因此阻止我读取数据。

我要怎么做才能从上面指定的表格中读出期刊名称和出版商?

【问题讨论】:

    标签: python html python-3.x web-scraping lxml


    【解决方案1】:

    就像您在问题中提到的那样,此表会动态更改 javascript。要解决这个问题,您实际上必须使用以下方法渲染 javascript

    • selenium 这样的网络驱动程序,它以与用户看起来相同的方式模拟网站(通过呈现 javascript)
    • requests-html,这是一个相对较新的模块,可让您在网页上呈现 javascript,并具有许多其他令人惊叹的网页抓取功能

    这是使用 requests-html 解决问题的一种方法:

    from requests_html import HTMLSession
    
    ISSN = "0897-4756"
    address = "https://www.journalguide.com/journals/search?type=journal-name&journal-name={}".format(ISSN)
    
    hdr = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'Accept-Encoding': 'none',
        'Accept-Language': 'en-US,en;q=0.8',
        'Connection': 'keep-alive'
    }
    
    ses = HTMLSession()
    response = ses.get(address, headers=hdr)
    response.html.render() # render the javascript to load the elements in the table
    tree = response.html.lxml # no need to import lxml.html because requests-html can do this for you
    
    print(tree.xpath('//table[@id="journal-search-results-table"]/text()'))
    # >> ['\n', '\n']
    
    print(tree.xpath('//table[@id="journal-search-results-table"]//td/text()'))
    # >> ['ACS Publications', '1.905', 'No', '\n', '\n', '\n']
    

    【讨论】:

      猜你喜欢
      • 2011-10-16
      • 2018-11-15
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2015-06-10
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多