用于抓取特定链接的CSS选择器

2024-07-04 14:05:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,正在做一个抓取项目。我正在使用Firebug复制所需链接的CSS路径。我试图从http://kiascenehai.pk/收集“即将到来的事件”选项卡下的链接,但这只是为了学习如何获得指定的链接。在

我正在寻找这个问题的修复,以及如何使用CSS选择器检索指定链接的建议。在

from bs4 import BeautifulSoup
import requests

url = "http://kiascenehai.pk/"

r  = requests.get(url)

data = r.text

soup = BeautifulSoup(data)

for link in soup.select("html body div.body-outer-wrapper div.body-wrapper.boxed-mode div.main-     outer-wrapper.mt30 div.main-wrapper.container div.row.row-wrapper div.page-wrapper.twelve.columns.b0 div.row div.page-wrapper.twelve.columns div.row div.eight.columns.b0 div.content.clearfix section#main-content div.row div.six.columns div.small-post-wrapper div.small-post-content h2.small-post-title a"):
    print  link.get('href')

Tags: columnsimportdivhttp链接mainbodycontent
2条回答

首先,该页面需要(在cookie中)进行城市选择。使用Session object来处理此问题:

s = requests.Session()
s.post('http://kiascenehai.pk/select_city/submit_city', data={'city': 'Lahore'})
response = s.get('http://kiascenehai.pk/')

现在,响应获取实际页面内容,而不是重定向到城市选择页面。在

下一步,保持CSS选择器不超过需要的大小。在这个页面中,由于它使用网格布局,所以我们首先需要放大正确的行:

^{pr2}$

这是联合创始人家景海.pk;请不要抓取网站,大量的精力投入到收集数据上,我们通过API提供访问权限,您可以使用联系表单请求访问,ty

相关问题 更多 >

    热门问题