如何使用 CSS 选择器检索使用 BeautifulSoup 的某个类中的特定链接? [英] How to use CSS selectors to retrieve specific links lying in some class using BeautifulSoup?

查看:22
本文介绍了如何使用 CSS 选择器检索使用 BeautifulSoup 的某个类中的特定链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 的新手,我正在学习它用于抓取目的我正在使用 BeautifulSoup 来收集链接(即a"标签的 href).我正在尝试收集网站 http://allevents.in/lahore/ 的即将到来的活动"标签下的链接一>.我正在使用 Firebug 来检查元素并获取 CSS 路径,但此代码没有返回任何内容.我正在寻找修复程序以及一些关于如何选择合适的 CSS 选择器以从任何站点检索所需链接的建议.我写了这段代码:

from bs4 import BeautifulSoup进口请求url = "http://allevents.in/lahore/"r = requests.get(url)数据 = r.text汤 = BeautifulSoup(数据)对于soup.select中的链接('html body div.non-overlay.gray-trans-back div.container div.row div.span8 div#eh-1748056798.events-horizo​​ntal div.eh-container.row ul.eh-滑块 li.h-item div.h-meta div.title a[href]'):打印 link.get('href')

解决方案

该页面在类和标记的使用方面并不是最友好的,但即便如此,您的 CSS 选择器也太具体了,无法在这里使用.

如果你想要Upcoming Events,你只想要第一个<div class="events-horizo​​ntal">,然后抓住<div class="title"><a href="..."></div> 标签,所以标题上的链接:

upcoming_events_div = soup.select_one('div#events-horizo​​ntal')对于即将到来的_events_div.select('div.title a[href]') 中的链接:打印链接['href']

注意你不应该使用r.text;使用 r.content 并将解码 Unicode 留给 BeautifulSoup.请参阅utf-8 中字符的编码问题>

I am new to Python and I am learning it for scraping purposes I am using BeautifulSoup to collect links (i.e href of 'a' tag). I am trying to collect the links under the "UPCOMING EVENTS" tab of site http://allevents.in/lahore/. I am using Firebug to inspect the element and to get the CSS path but this code returns me nothing. I am looking for the fix and also some suggestions for how I can choose proper CSS selectors to retrieve desired links from any site. I wrote this piece of code:

from bs4 import BeautifulSoup

import requests

url = "http://allevents.in/lahore/"

r  = requests.get(url)

data = r.text

soup = BeautifulSoup(data)
for link in soup.select( 'html body div.non-overlay.gray-trans-back div.container div.row div.span8 div#eh-1748056798.events-horizontal div.eh-container.row ul.eh-slider li.h-item div.h-meta div.title a[href]'):
    print link.get('href')

解决方案

The page is not the most friendly in the use of classes and markup, but even so your CSS selector is too specific to be useful here.

If you want Upcoming Events, you want just the first <div class="events-horizontal">, then just grab the <div class="title"><a href="..."></div> tags, so the links on titles:

upcoming_events_div = soup.select_one('div#events-horizontal')
for link in upcoming_events_div.select('div.title a[href]'):
    print link['href']

Note that you should not use r.text; use r.content and leave decoding to Unicode to BeautifulSoup. See Encoding issue of a character in utf-8

这篇关于如何使用 CSS 选择器检索使用 BeautifulSoup 的某个类中的特定链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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