我正在试图解析这个网站上的HTML。
我想用span从所有这些class = "post-subject"元素中获取文本
示例:
<span class="post-subject">Set of 20 moving boxes (20009 or 20011)</span>
<span class="post-subject">Firestick/Old xbox games</span>当我在下面运行我的代码时,soup.find()返回None。我不知道怎么回事?
import requests
from bs4 import BeautifulSoup
page = requests.get('https://trashnothing.com/washington-dc-freecycle?page=1')
soup = BeautifulSoup(page.text, 'html.parser')
soup.find('span', {'class': 'post-subject'})发布于 2018-07-26 01:15:28
为了帮助您开始工作,应该加载页面,您需要获得正确的壁虎司机,然后可以使用Selenium实现。我没有看到一个类:在您链接的页面上发布主题,但是您可以自动单击登录按钮,如下所示:
availbutton = driver.find_element_by_id('buttonAvailability_1')
availbutton.click()from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://trashnothing.com/washington-dc-freecycle?page=1')
html = driver.page_source
soup = BeautifulSoup(html,'lxml')
print(soup.find('span', {'class': 'post-subject'}))发布于 2020-08-28 15:00:54
我也有过同样的问题。刚刚把html.parser改成了html5lib和boom。那时候起作用了。另外,使用soup.find_all()而不是soup.find()作为返回多个对象的函数是一个很好的实践。
https://stackoverflow.com/questions/51529482
复制相似问题