【发布时间】: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 成功实现自动化
我曾尝试使用httplib2、requests、urllib2 和lxml.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 元素,因此它包含的 tr 和 td 元素在 python 解释 HTML 时没有被加载 - 这是因此阻止我读取数据。
我要怎么做才能从上面指定的表格中读出期刊名称和出版商?
【问题讨论】:
标签: python html python-3.x web-scraping lxml