无法使用BeautifulSoup CSS选择器选择HTML元素,但能够使用CSS选择器在JS中获取元素 [英] Unable to select HTML element using BeautifulSoup CSS-selector but was able to get the element in JS using CSS-selectors

查看:220
本文介绍了无法使用BeautifulSoup CSS选择器选择HTML元素,但能够使用CSS选择器在JS中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和BeautfulSoup HTML解析器来选择HTML元素.但是,我无法正常工作.

I am using Python and the BeautfulSoup HTML parser to select HTML elements. However, I'm unable to get this working.

response = requests_session.post(login_url, headers=headers, data=data_credentials) # log in to the requests Session so that you can reuse it

search_url= 'https://www.website.com/search.php'
p_id='342953'

response = requests_session.get(search_url,headers=headers, params={'query':p_id,'type':'p'})
redirected_urls=response.url
th_soup = BeautifulSoup(response.content, 'html.parser')
trx_ht =th_soup.select("body > table > tbody > tr > td > table > tbody > tr:nth-child(2) > td:nth-child(2) > div:nth-child(3) > table > tbody > tr:nth-child(11) > td > table > tbody > tr:nth-child(4) > td:nth-child(5) > input[type='hidden']:nth-child(1)")

推荐答案

从您在pastebin中提供的HTML中,可以使用具有特定属性的.find_all()调用来定位隐藏的输入.如果您想要的字段始终以qtyb-开头,则可以对BeautifulSoup使用正则表达式来查找所有匹配元素,如下所示:

From the HTML you have given in your pastebin, the hidden input could be located using a .find_all() call with specific attributes. If the field you want always starts qtyb-, you can use a regular expression with BeautifulSoup to find all matching elements as follows:

from bs4 import BeautifulSoup
import re

# Read the HTML in from a file (normally requests is used)

with open('sm7iXcUq.html', encoding='utf-8') as f_html:
    html = f_html.read()

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

for i in soup.find_all('input', attrs={'type' : 'hidden', 'name' : re.compile('qtyb-.*')}):
    print(i)

对于您提供的HTML,这将返回一个元素,如下所示:

For the HTML you have given, this would return one element as follows:

<input name="qtyb-52843099" type="hidden" value="1"/>

name的值可以使用:

i['name']

这种方法将为您提供具有匹配的name的所有元素.

This approach would give you all elements with the matching name.

这篇关于无法使用BeautifulSoup CSS选择器选择HTML元素,但能够使用CSS选择器在JS中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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