Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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 获取HTML href链接,该链接与包含Beautiful Soup的字符串列表中的字符串相匹配_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 获取HTML href链接,该链接与包含Beautiful Soup的字符串列表中的字符串相匹配

Python 获取HTML href链接,该链接与包含Beautiful Soup的字符串列表中的字符串相匹配,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我正在尝试从包含URL列表的网页获取URL。我不想得到所有的URL,只有那些文本与列表中字符串文本匹配的URL。字符串列表是网页上链接文本的子集,我通过抓取页面并删除我不想要的文本来提取。我有一个字符串列表存储在文件名中 我正在尝试提取列表中包含字符串的链接。下面返回一个空列表 r=requests.get(url) soup=BeautifulSoup(r.content,'html5lib') links=soup.findAll('a',string=filename[0]) 文件链接=[

我正在尝试从包含URL列表的网页获取URL。我不想得到所有的URL,只有那些文本与列表中字符串文本匹配的URL。字符串列表是网页上链接文本的子集,我通过抓取页面并删除我不想要的文本来提取。我有一个字符串列表存储在
文件名中

我正在尝试提取列表中包含字符串的链接。下面返回一个空列表

r=requests.get(url)
soup=BeautifulSoup(r.content,'html5lib')
links=soup.findAll('a',string=filename[0])
文件链接=[link['href']如果在链接['href']中“导出”,则链接中的链接为[link['href']
标记看起来像这样:

我想获得前三个的href链接,但不是最后一个,因为字符串
“ECZ公民教育论文2 2009”。
不在我的字符串列表中。该网站的链接是

我的字符串列表如下所示:


文件名=['ECZ数学论文2 2019','ECZ数学论文2 2019',
“ECZ科学论文3 2009”。]

我只想要前三个链接,因为链接的文本在我的列表中(文件名)。我不想要第四个链接,因为href链接(ECZ Civil Education Paper 2 2009)旁边的文本不在我的列表中,因为我不想下载此文件。

如果请求已成功接收。然后使用bs解析它,并使用findAll查找链接“a”的标记。我认为没有必要在findAll中传递(string=filenames[0])

from bs4 import BeautifulSoup as bs
temp = """<p><a href="https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi">
                            ECZ Mathematics Paper 2 2019.</a></p>

<p><a href="https://drive.google.com/uc?export=download&id=1x_9E3PaviCuSsqfJqOsQKOwVlCWZ1jqf">
                            ECZ Mathematics Paper 1 2019.</a></p>

<p><a href="https://drive.google.com/uc?export=download&id=1QFOzpPLuQPup8FtKgOoIcvzTnzCaRzUp">
                            ECZ Science Paper 3 2009.</a></p>

<p><a href="https://drive.google.com/uc?export=download&id=0B0lFc6TrfIg7aENYc1V6akRVVnc">
                            ECZ Civic Education Paper 2 2009.</a></p>"""

soup =bs(temp, 'html5lib')
links = soup.findAll('a')
file_links = [link['href'] for link in links if "export" in link['href']]

用这种方法试试看是否有效:

   html = """    
    <p><a href="https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi">
                                ECZ Mathematics Paper 2 2019.</a></p>    
    <p><a href="https://drive.google.com/uc?export=download&id=1x_9E3PaviCuSsqfJqOsQKOwVlCWZ1jqf">
                                ECZ Mathematics Paper 1 2019.</a></p>    
    <p><a href="https://drive.google.com/uc?export=download&id=1QFOzpPLuQPup8FtKgOoIcvzTnzCaRzUp">
                                ECZ Science Paper 3 2009.</a></p>    
    <p><a href="https://drive.google.com/uc?export=download&id=0B0lFc6TrfIg7aENYc1V6akRVVnc">
                                ECZ Civic Education Paper 2 2009.</a></p>   
   """
    filenames = ['ECZ Mathematics Paper 2 2019.', 'ECZ Mathematics Paper 2 2019.',
                 'ECZ Science Paper 3 2009.']

    soup = bs(html, 'html5lib')

    all_links = soup.findAll('a')

    for link in all_links:           
        for nam in filenames:                
            if link.text.strip()==nam:
                print(link['href'])

您可以构造CSS选择器,然后一次性选择链接。例如(
html
是问题中的代码片段):

印刷品:

https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi
https://drive.google.com/uc?export=download&id=1x_9E3PaviCuSsqfJqOsQKOwVlCWZ1jqf
https://drive.google.com/uc?export=download&id=1QFOzpPLuQPup8FtKgOoIcvzTnzCaRzUp

你能从你的stings列表中发布几个例子吗?我已经编辑了这篇文章,包括我的列表的一个例子。我只想要前三个链接,因为链接的文本在我的列表(文件名)中。我不想要第四个链接,因为href链接(ECZ Civil Education Paper 2 2009)旁边的文本不在我的列表中,因为我不想下载此文件。这非常有效,但我已经接受了答案。谢谢你。
https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi
https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi
https://drive.google.com/uc?export=download&id=1QFOzpPLuQPup8FtKgOoIcvzTnzCaRzUp
filenames = ['ECZ Mathematics Paper 1 2019.',
             'ECZ Mathematics Paper 2 2019.',
             'ECZ Science Paper 3 2009.']

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')

for a in soup.select(','.join('a:contains("{}")'.format(i) for i in filenames)):
    print(a['href'])
https://drive.google.com/uc?export=download&id=1wVjbdN9fztrjxhONGRX5U6N1OJDAChOi
https://drive.google.com/uc?export=download&id=1x_9E3PaviCuSsqfJqOsQKOwVlCWZ1jqf
https://drive.google.com/uc?export=download&id=1QFOzpPLuQPup8FtKgOoIcvzTnzCaRzUp