小编Fle*_*cha的帖子

如何使用CSS选择器使用BeautifulSoup检索位于某个类中的特定链接?

我是Python的新手,我正在学习它用于抓取目的我使用BeautifulSoup来收集链接(即'a'标签的href).我正在尝试收集网站http://allevents.in/lahore/的"UPCOMING EVENTS"标签下的链接.我正在使用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')
Run Code Online (Sandbox Code Playgroud)

css python firebug beautifulsoup css-selectors

16
推荐指数
3
解决办法
4万
查看次数

标签 统计

beautifulsoup ×1

css ×1

css-selectors ×1

firebug ×1

python ×1