gpt4 book ai didi

python:从很长的div类输出中选择特定部分

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:29 24 4
gpt4 key购买 nike

我正在从网站提取信息,输出很长。我怎样才能只选择我感兴趣的关键部分并将其分配给一个新对象

这是我用来提取信息的代码部分 -

soup = bs(response.text,"html.parser")
cartl = soup.find("div",{"class":"product-view"})
cart_link = cartl.find_all("form")

这是我的长输出(我将其缩短,因为它提取的全文大约有 100 行)-

<form action="https://www.randomsite.com/checkout/cart/add/uenc/aHR0cHM6Ly93d3cudGhlZ29vZHdpbGxvdXQuY29tL25pa2UtYWlyLWpvcmRhbi0xMy1yZXRyby1iZy1oaXN0b3J5LW9mLWZsaWdodC13aGl0ZS1tZXRhbGljLXNpbHZlci11bml2ZXJzaXR5LXJlZC00MTQ1NzQtMTAzP19fX1NJRD1V/product/92797/form_key/NBlK6IE3LYdwf0Vh/" id="product_addtocart_form" method="post">
<input name="form_key" type="hidden" value="NBlK6IE3LYdwf0Vh"/>
<div class="no-display">
<input name="product" type="hidden" value="92797"/>
<input id="related-products-field" name="related_product" type="hidden" value=""/>
</div>

我想将此添加到一个新对象中 - https://www.randomsite.com/checkout/cart/add/uenc/aHR0cHM6Ly93d3cudGhlZ29vZHdpbGxvdXQuY29tL25pa2UtYWlyLWpvcmRhbi0xMy1yZXRyby1iZy1oaXN0b3J5LW9mLWZsa WdodC13aGl0ZS1tZXRhbGljLXNpbHZlci11bml2ZXJzaXR5LXJlZC00MTQ1NzQtMTAzP19fX1NJRD1V/product/92797/form_key/NBlK6IE3LYdwf0Vh/

这是通过下面的答案更新的新代码,谢谢 -

from bs4 import BeautifulSoup
import requests

session = requests.session()
endpoint = "https://randomsite.com/"
response = session.get(endpoint)

soup0 = BeautifulSoup(response.text,"html.parser")

div = soup0.find("div",{"class":"product-view"})
html = div.find("form")

soup = BeautifulSoup(html, 'html.parser')
form = soup.find('form', { 'id': 'product_addtocart_form' })
action = form['action']
print(action)

这是一个新错误,我知道我哪里出了问题 -

Traceback (most recent call last):
File "test.py", line 16, in <module>
soup = BeautifulSoup(html, 'html.parser')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bs4/__init__.py", line 191, in __init__
markup = markup.read()
TypeError: 'NoneType' object is not callable

最佳答案

您可以使用BeautifulSoup find方法来获取对 <form> 的引用标签(如果页面上有多个表单,则可以选择过滤特定的id)。然后,将表单对象视为字典来提取 action属性。

代码

from bs4 import BeautifulSoup

html = '''
<form action="https://www.randomsite.com/checkout/cart/add/uenc/aHR0cHM6Ly93d3cudGhlZ29vZHdpbGxvdXQuY29tL25pa2UtYWlyLWpvcmRhbi0xMy1yZXRyby1iZy1oaXN0b3J5LW9mLWZsaWdodC13aGl0ZS1tZXRhbGljLXNpbHZlci11bml2ZXJzaXR5LXJlZC00MTQ1NzQtMTAzP19fX1NJRD1V/product/92797/form_key/NBlK6IE3LYdwf0Vh/" id="product_addtocart_form" method="post">
<input name="form_key" type="hidden" value="NBlK6IE3LYdwf0Vh"/>
<div class="no-display">
<input name="product" type="hidden" value="92797"/>
<input id="related-products-field" name="related_product" type="hidden" value=""/>
</div>
'''

soup = BeautifulSoup(html, 'html.parser')
form = soup.find('form', { 'id': 'product_addtocart_form' })
action = form['action']
print action

输出

https://www.randomsite.com/checkout/cart/add/uenc/aHR0cHM6Ly93d3cudGhlZ29vZHdpbGxvdXQuY29tL25pa2UtYWlyLWpvcmRhbi0xMy1yZXRyby1iZy1oaXN0b3J5LW9mLWZsaWdodC13aGl0ZS1tZXRhbGljLXNpbHZlci11bml2ZXJzaXR5LXJlZC00MTQ1NzQtMTAzP19fX1NJRD1V/product/92797/form_key/NBlK6IE3LYdwf0Vh/

关于python:从很长的div类输出中选择特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com