【发布时间】:2018-07-26 00:46:43
【问题描述】:
我正在尝试解析此 website 上的 HTML。
我想用class = "post-subject"从所有这些span元素中获取文本
例子:
<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'})
【问题讨论】:
-
如果您在该页面上运行
document.querySelector('.post-subject'),也不会返回任何内容。你在哪里看到类为post-subject的元素?您是否必须先运行搜索或与页面交互?如果是这样,您需要在调用 BeautifulSoup 之前执行此操作... -
该页面需要登录。查看
mechanize或Selenium,了解以交互方式访问网页的方法。
标签: python web-scraping beautifulsoup