How to use CSS selectors to retrieve specific links lying in some class using BeautifulSoup?(如何使用 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-horizontal">,然后抓住 <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 检索位于某个类中的特定链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 CSS 选择器使用 BeautifulSoup 检索位于某个
基础教程推荐
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
