Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 beautifulSoup.select()为css选择器返回空值_Python_Web Scraping_Beautifulsoup_Python 3.7 - Fatal编程技术网

Python beautifulSoup.select()为css选择器返回空值

Python beautifulSoup.select()为css选择器返回空值,python,web-scraping,beautifulsoup,python-3.7,Python,Web Scraping,Beautifulsoup,Python 3.7,我正在尝试解析这个站点的一些链接 我想选择一个特定的表 document.querySelector("#hnmain > tbody > tr:nth-child(3) > td > table") 我知道bs4有css选择器限制。但问题是我甚至不能选择像hnmain>tbody with soup这样简单的选项 使用下面的代码,我无法解析tbody,而我使用js做了屏幕截图 from bs4 import BeautifulSoup import requests

我正在尝试解析这个站点的一些链接

我想选择一个特定的表

document.querySelector("#hnmain > tbody > tr:nth-child(3) > td > table")
我知道bs4有css选择器限制。但问题是我甚至不能选择像hnmain>tbody with soup这样简单的选项

使用下面的代码,我无法解析tbody,而我使用js做了屏幕截图

from bs4 import BeautifulSoup
import requests
print("-"*100)
print("Hackernews parser")
print("-"*100)
url="https://news.ycombinator.com/"
res=requests.get(url)
html=res.content
soup=BeautifulSoup(html)
table=soup.select('#hnmain > tbody')
print(table)
输出:


与其浏览正文和表格,为什么不直接浏览链接?我对此进行了测试,效果良好:

links=soup.select('a',{'class':'storylink'})
如果您想要表格,因为每页只有一个,您也不需要遍历其他元素—您可以直接转到它

table = soup.select('table')

与其浏览正文和表格,为什么不直接浏览链接?我对此进行了测试,效果良好:

links=soup.select('a',{'class':'storylink'})
如果您想要表格,因为每页只有一个,您也不需要遍历其他元素—您可以直接转到它

table = soup.select('table')

我没有从beautifulsoup或curl脚本获得html标记tbody。 这意味着

soup.select('tbody')
返回空列表。这与您获取空列表的原因相同

要提取您正在查找的链接,只需执行以下操作

soup.select("a.storylink")

它将从站点获取您想要的链接。

我没有从beautifulsoup或curl脚本获取html标记tbody。 这意味着

soup.select('tbody')
返回空列表。这与您获取空列表的原因相同

要提取您正在查找的链接,只需执行以下操作

soup.select("a.storylink")

它将从站点获取您想要的链接。

数据以3行为一组排列,其中第三行是用于间距的空行。循环最上面的行,并使用next_同级在每个点获取关联的第二行。bs4.7.1+

from bs4 import BeautifulSoup as bs
import requests

r = requests.get('https://news.ycombinator.com/')
soup = bs(r.content, 'lxml')
top_rows = soup.select('.athing')

for row in top_rows:
    title = row.select_one('.storylink')
    print(title.text)
    print(title['href'])
    print('https://news.ycombinator.com/' + row.select_one('.sitebit a')['href'])
    next_row = row.next_sibling
    print(next_row.select_one('.score').text)
    print(next_row.select_one('.hnuser').text)
    print(next_row.select_one('.age a').text)
    print(next_row.select_one('a:nth-child(6)').text)
    print(100*'-')

数据以3行为一组排列,其中第三行是用于间距的空行。循环最上面的行,并使用next_同级在每个点获取关联的第二行。bs4.7.1+

from bs4 import BeautifulSoup as bs
import requests

r = requests.get('https://news.ycombinator.com/')
soup = bs(r.content, 'lxml')
top_rows = soup.select('.athing')

for row in top_rows:
    title = row.select_one('.storylink')
    print(title.text)
    print(title['href'])
    print('https://news.ycombinator.com/' + row.select_one('.sitebit a')['href'])
    next_row = row.next_sibling
    print(next_row.select_one('.score').text)
    print(next_row.select_one('.hnuser').text)
    print(next_row.select_one('.age a').text)
    print(next_row.select_one('a:nth-child(6)').text)
    print(100*'-')

那一页总共有三张桌子哦,是的,我的错。那么,你可以根据类ID或其他内容进行解析,但如果我是你,我就不会遍历属性层次结构。你的方法对于获取主要故事链接很好,但我需要从每篇文章中获取更多属性,如投票计数、评论计数、站点url,这些项目在没有特定类的情况下是span/a的。请帮助有类ID为“subtext”的属性,其中包含大量帖子信息。span属性也有ID,具体取决于存储的信息-仔细查看HTML并找到您想要获取的信息的模式。该页面中总共有3个表哦,是的,我的错。那么,你可以根据类ID或其他内容进行解析,但如果我是你,我就不会遍历属性层次结构。你的方法对于获取主要故事链接很好,但我需要从每篇文章中获取更多属性,如投票计数、评论计数、站点url,这些项目在没有特定类的情况下是span/a的。请帮助有类ID为“subtext”的属性,其中包含大量帖子信息。span属性也有ID,具体取决于存储的信息-仔细查看HTML,找到您想要获取的信息的模式。谢谢!!您的方法对于获取主要故事链接很好,但我需要从每篇文章中获取更多属性,如upvote\u count、comment\u count、url\u to\u right\u of\u main\u url、posted\u ago,其中这些项目是span/a,没有特定的类。请帮忙!非常感谢。您的方法对于获取主要故事链接很好,但我需要从每篇文章中获取更多属性,如upvote\u count、comment\u count、url\u to\u right\u of\u main\u url、posted\u ago,其中这些项目是span/a,没有特定的类。请帮忙!