Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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 美丽的群像,一个接一个的芬德尔?_Python_Beautifulsoup_Python Requests - Fatal编程技术网

Python 美丽的群像,一个接一个的芬德尔?

Python 美丽的群像,一个接一个的芬德尔?,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我对Python非常陌生,主要需要它来从网站获取信息。 在这里,我试图从网站底部获取简短的标题,但无法完全获取 from bfs4 import BeautifulSoup import requests url = "http://some-website" r = requests.get(url) soup = BeautifulSoup(r.content, "html.parser") nachrichten = soup.findAll('ul', {'class':'list'

我对Python非常陌生,主要需要它来从网站获取信息。 在这里,我试图从网站底部获取简短的标题,但无法完全获取

from bfs4 import BeautifulSoup
import requests

url = "http://some-website"
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")

nachrichten = soup.findAll('ul', {'class':'list'})
现在我需要另一个findAll来从var“nachrichten”获取所有链接/a,但是我如何才能做到这一点呢?

如果希望所有链接都在一个列表中,请使用带有select的css选择器:

anchors = soup.select('ul.list a')
如果您想要单独的列表:

anchors = [ ul.find_all(a) for a in soup.find_all('ul', {'class':'list'})]
此外,如果您想要href,您可以确保只找到具有href属性的锚,并提取:

hrefs = [a["href"] for a in soup.select('ul.list a[href]')]
使用
find_all
set href=True,即
ul.find_all(a,href=True)


希望这能解决你的问题,我认为进口的是bs4。我从来没有见过bfs4,我认为没有bfs4。我从来没有这样做过。
from bs4 import BeautifulSoup
import requests
url = "http://www.n-tv.de/ticker/"
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")
nachrichten = soup.findAll('ul', {'class':'list'})
links = []
for ul in nachrichten:
    links.extend(ul.findAll('a'))
print len(links)