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

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

问题描述

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

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')

推荐答案

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

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.

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

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']

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

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天全站免登陆