Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “美丽集团”;findAll();不返回标签_Python_Url_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Python “美丽集团”;findAll();不返回标签

Python “美丽集团”;findAll();不返回标签,python,url,web-scraping,beautifulsoup,python-requests,Python,Url,Web Scraping,Beautifulsoup,Python Requests,我正试图建立一个刮刀,以获得一些学术论文的摘要及其相应的标题 问题是我的对于bsObj.findAll('a',{'class':'search-track'})中的链接,没有返回我进一步构建scraper所需的链接。在我的代码中,支票如下所示: for link in bsObj.findAll('a',{'class':'search-track'}): print(link) 上面的for循环不会打印任何内容,但是href链接应该在中,但是更改Beautifulsoup解析器并

我正试图建立一个刮刀,以获得一些学术论文的摘要及其相应的标题

问题是我的
对于bsObj.findAll('a',{'class':'search-track'})中的链接,
没有返回我进一步构建scraper所需的链接。在我的代码中,支票如下所示:

for link in bsObj.findAll('a',{'class':'search-track'}):
     print(link)
上面的for循环不会打印任何内容,但是
href
链接应该在
中,但是更改Beautifulsoup解析器并不能解决我的代码问题。我在我的Beautifulsoup构造函数中使用了
“html.parser”
bsObj=bs(html.content,features=“html.parser”)

print(len(bsObj))
“lxml”
“html5lib”
打印“3”,同时打印“2”

另外,我开始使用
urllib.request.urlopen
获取页面,然后尝试使用
requests.get()
。不幸的是,这两种方法给了我相同的
bsObj

以下是我编写的代码:

#from urllib.request import urlopen
import requests
from bs4 import BeautifulSoup as bs
import ssl


'''
The elsevier search is kind of a tree structure:
"keyword --> a list of journals (a journal contain many articles) --> lists of articles
'''
address = input("Please type in your keyword: ") #My keyword is catalyst for water splitting
#https://www.elsevier.com/en-xs/search-results? 
#query=catalyst%20for%20water%20splitting&labels=journals&page=1
address = address.replace(" ", "%20")
address = "https://www.elsevier.com/en-xs/search-results?query=" + address + "&labels=journals&page=1"

journals = []
articles = []

def getJournals(url):
    global journals

    #html = urlopen(url)
    html = requests.get(url)
    bsObj = bs(html.content, features="html.parser")

    #print(len(bsObj))
    #testFile = open('testFile.txt', 'wb')
    #testFile.write(bsObj.text.encode(encoding='utf-8', errors='strict') +'\n'.encode(encoding='utf-8', errors='strict'))
    #testFile.close()

    for link in bsObj.findAll('a',{'class':'search-track'}):
        print(link) 
        ########does not print anything########
        '''
        if 'href' in link.attrs and link.attrs['href'] not in journals:
            newJournal = link.attrs['href']
            journals.append(newJournal)
        '''
    return None


# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

getJournals(address)
print(journals)

有人能告诉我for循环没有打印出任何链接的代码中有什么问题吗?我需要将期刊的链接存储在一个列表中,然后访问每个链接以获取论文摘要。没错,论文的摘要部分是免费的,网站不应该因为它而阻止我的ID。

这个页面是动态加载jscript的,所以Beautifulsoup不能直接处理它。您可以使用Selenium来实现,但在本例中,您可以通过跟踪页面发出的api调用来实现(

在您的特殊情况下,可以通过以下方式完成:

from bs4 import BeautifulSoup as bs
import requests
import json

#this is where the data is hiding:
url = "https://site-search-api.prod.ecommerce.elsevier.com/search?query=catalyst%20for%20water%20splitting&labels=journals&start=0&limit=10&lang=en-xs"
html = requests.get(url)
soup = bs(html.content, features="html.parser")


data = json.loads(str(soup))#response is in json format so we load it into a dictionary
注意:在这种情况下,也可以完全不用Beautifulsoup,直接加载响应,如
data=json.loads(html.content)

hits = data['hits']['hits']#target urls are hidden deep inside nested dictionaries and lists
for hit in hits:
    print(hit['_source']['url'])
输出:

https://www.journals.elsevier.com/water-research
https://www.journals.elsevier.com/water-research-x

等等。

此页面是用jscript动态加载的,因此Beautifulsoup无法直接处理它。您可以使用Selenium来完成此操作,但在这种情况下,您可以通过跟踪页面的api调用来完成(

在您的特殊情况下,可以通过以下方式完成:

from bs4 import BeautifulSoup as bs
import requests
import json

#this is where the data is hiding:
url = "https://site-search-api.prod.ecommerce.elsevier.com/search?query=catalyst%20for%20water%20splitting&labels=journals&start=0&limit=10&lang=en-xs"
html = requests.get(url)
soup = bs(html.content, features="html.parser")


data = json.loads(str(soup))#response is in json format so we load it into a dictionary
注意:在这种情况下,也可以完全不用Beautifulsoup,直接加载响应,如
data=json.loads(html.content)

hits = data['hits']['hits']#target urls are hidden deep inside nested dictionaries and lists
for hit in hits:
    print(hit['_source']['url'])
输出:

https://www.journals.elsevier.com/water-research
https://www.journals.elsevier.com/water-research-x

等等。

你能简单地发布
html=requests.get(url)
?@JackFleeting链接在这里:你能简单地发布
html=requests.get(url)使用的确切url吗
?@JackFleeting链接在这里:哦,非常感谢您的代码修复了它!!但是由于此API方法不使用原始URL链接,并且此处显示的链接只返回第一页的结果,我如何继续抓取搜索结果页的第二、第三页?很抱歉,我对API操作不是很熟悉,但是我可以问一下ho吗w
站点搜索api.prod.ecommerce
api可以从站点获取信息,而不是使用由Elsevier生成的api密钥?哦,我想我找到了解决问题的方法!我通过增加
&limit=0
中的数量刮取了更多的URL。非常感谢你的灵感!@DanielQiao很高兴它有帮助!是的,还有still关于动态加载页面还有很多需要学习的地方…祝你好运。顺便说一句,如果你完成了,别忘了接受答案。哦,非常感谢你的代码修复了它!!但是由于这个API方法不使用原始URL链接,并且这里显示的链接只返回第一页的结果,我怎么能继续删除sear的第二、第三页呢ch结果页面?很抱歉,我不太熟悉API操作,但是我可以问一下
站点搜索API.prod.ecommerce
API如何从站点获取信息,而不是使用Elsevier生成的API密钥?哦,我想我已经找到了解决问题的方法!我通过增加
&limit=0
中的数量刮取了更多的URL。非常感谢你的灵感!@DanielQiao很高兴它能帮上忙!是的,关于动态加载页面还有很多东西需要学习……祝你好运。顺便说一句,如果你完成了,别忘了接受答案。