python - beautifulsoup - 无法从列表中获取信息
问题描述
总蟒蛇业余爱好者在这里。我已经为一些表面级的东西上过几节课,但还没有为我正在尝试抓取的新网站找到解决这个问题的方法(kijiji.ca 供任何想知道的人使用)。我正在尝试为一些与博士论文相关的工作拉下有关出租房屋的信息。检查示例页面,我发现我需要的一些关键信息都具有相同的类。例如:
<div class="titleAttributes-2381855425">
<li class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-unittype"></use></svg><span class="noLabelValue-3861810455">Condo</span></li>
<li
class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbedrooms"></use></svg><span class="noLabelValue-3861810455">Bedrooms: 2</span></li>
<li
class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbathrooms"></use></svg><span class="noLabelValue-3861810455">Bathrooms: 1</span></li>
</div>
我正在尝试获取每条信息,但是当我运行我的代码时,它什么也没有显示。
def getDetails(urls):
urls = urls[10:]
print(len(urls))
i =0;
try:
for url in urls:
print(url)
listDetails = ""
listDetailsTwo = []
url = url.rstrip('\n')
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
try:
infobar = soup.select_one("span[class*=noLabelValue-3861810455]").text
infobar.append(infobar)
print("Scraping listing : ",str(i))
(我知道,我的代码一定看起来一团糟,但我还是个完全的业余爱好者。)我知道我必须使用非 soup.select_one 的东西,但经过几天的尝试,我真的一无所获。任何帮助将非常感激!
谢谢!
解决方案
尝试这个。
from simplified_scrapy import SimplifiedDoc,req,utils
html='''
<div class="titleAttributes-2381855425">
<li class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-unittype"></use></svg><span class="noLabelValue-3861810455">Condo</span></li>
<li
class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbedrooms"></use></svg><span class="noLabelValue-3861810455">Bedrooms: 2</span></li>
<li
class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbathrooms"></use></svg><span class="noLabelValue-3861810455">Bathrooms: 1</span></li>
</div>'''
doc = SimplifiedDoc(html)
spans = doc.selects('span.noLabelValue-3861810455>text()')
print (spans)
spans = doc.selects('div.titleAttributes-2381855425>li.noLabelAttribute-1492730675').select('span.noLabelValue-3861810455>text()')
print (spans)
utils.save2csv('test.csv',[spans])
结果:
['Condo', 'Bedrooms: 2', 'Bathrooms: 1']
['Condo', 'Bedrooms: 2', 'Bathrooms: 1']
推荐阅读
- php - BASE64 被破坏的透明图像
- php - Android - 创建公钥对象以解码 JWT
- python - TemplateDoesNotExist at / .html
- c++ - 编译时多组的笛卡尔积
- machine-learning - colab 中使用 ffmpeg_extract_subclip 进行音频剪辑的问题
- python - model.predict() 输出对应于 3 个类中的哪一个?
- python - 如何检查字符串的某个部分是否在 Python 中的一组字符串中?
- matlab - 如何在matlab中绘制具有(31 * 1)维度的单元格数组?
- javascript - Javascript中未定义变量值
- data-structures - 将节点链接到链表的末尾是如何工作的?