将nth-child替换为nth-of-type会产生意外错误 [英] Replacement of nth-child to nth-of-type gives an unexpected error

查看:121
本文介绍了将nth-child替换为nth-of-type会产生意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过BeautifulSoup从以下网页中获取一些信息:

I am trying to get some information from the following web-page with BeautifulSoup:

url = 'https://web.archive.org/web/20071001215911/http://finance.rambler.ru'

借助浏览器(Chrome),我复制了所需元素的选择器:

With the help of my browser (Chrome), I copy the selector for the desired element:

selector = 'body > div.fe_global > table:nth-child(6) > tbody > tr > td:nth-child(2) > table > tbody > tr > td.fe_col-left > div:nth-child(5) > table > tbody'

但是,bs4不支持nth-child,因此我将其替换为nth-of-type:

However, bs4 does not support nth-child, thus I replace it with nth-of-type:

selector = selector.replace('child', 'of-type')

将其涂在汤上

r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
selected_element = soup.select(selector=selector)

print (selected_element)

输出为[]. 我期望得到一些HTML代码. 这样回答的原因是什么? 谢谢您的帮助.

the output is []. I expected to get some HTML code instead. What is the cause of such an answer? Thank you for your help.

推荐答案

在选定的div中,它有2个表,我将选择第二个表

In selected div it has 2 table and I will select second table

from bs4 import BeautifulSoup
import requests

url = 'https://web.archive.org/web/20071001215911/http://finance.rambler.ru'
heads = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0'}
r = requests.get(url, headers=heads)
soup = BeautifulSoup(r.text, 'html.parser')
selected_element = soup.select('div[class="fe_small fe_l2"] table')[1]

print (selected_element)

这篇关于将nth-child替换为nth-of-type会产生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆