Python 如何使用 CSS 选择器检索使用 BeautifulSoup 的某个类中的特定链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24801548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use CSS selectors to retrieve specific links lying in some class using BeautifulSoup?
提问by Flecha
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:
我是 Python 新手,我正在学习它用于抓取目的我正在使用 BeautifulSoup 来收集链接(即“a”标签的 href)。我正在尝试收集网站http://allevents.in/lahore/的“即将到来的活动”选项卡下的链接。我正在使用 Firebug 来检查元素并获取 CSS 路径,但此代码没有返回任何内容。我正在寻找修复程序以及一些关于如何选择合适的 CSS 选择器以从任何站点检索所需链接的建议。我写了这段代码:
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')
采纳答案by Martijn Pieters
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.
该页面在类和标记的使用方面并不是最友好的,但即便如此,您的 CSS 选择器也太具体了,无法在这里使用。
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,你只想要 first <div class="events-horizontal">
,然后抓住<div class="title"><a href="..."></div>
标签,所以标题上的链接:
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 notuse r.text
; use r.content
and leave decoding to Unicode to BeautifulSoup. See Encoding issue of a character in utf-8
请注意,您应不使用r.text
; 使用r.content
并将解码为 Unicode 留给 BeautifulSoup。请参阅utf-8 中字符的编码问题
回答by Anuj Saraswat
import bs4 , requests
res = requests.get("http://allevents.in/lahore/")
soup = bs4.BeautifulSoup(res.text)
for link in soup.select('a[property="schema:url"]'):
print link.get('href')
This code will work fine!!
这段代码可以正常工作!!
回答by Lavish Saluja
soup.select('div')
All elements named <div>
soup.select('#author')
The element with an id attribute of author
soup.select('.notice')
All elements that use a CSS class attribute named notice
soup.select('div span')
All elements named <span> that are within an element named <div>
soup.select('div > span')
All elements named <span> that are directly within an element named <div>, with no other element in between
soup.select('input[name]')
All elements named <input> that have a name attribute with any value
soup.select('input[type="button"]')
All elements named <input> that have an attribute named type with value button
You may also be interested by this book.
您可能也对本书感兴趣。