python 内置浏览器robobrowser_robobrowser

RoboBrowser: Your friendly neighborhood web scraper

RoboBrowser is a simple, Pythonic library for browsing the web without a

standalone web browser. RoboBrowser can fetch a page, click on links and

buttons, and fill out and submit forms. If you need to interact with web

services that don't have APIs, RoboBrowser can help.

import re

from robobrowser import RoboBrowser

# Browse to Genius

browser = RoboBrowser(history=True)

browser.open('http://genius.com/')

# Search for Porcupine Tree

form = browser.get_form(action='/search')

form #

form['q'].value = 'porcupine tree'

browser.submit_form(form)

# Look up the first song

songs = browser.select('.song_link')

browser.follow_link(songs[0])

lyrics = browser.select('.lyrics')

lyrics[0].text # \nHear the sound of music ...

# Back to results page

browser.back()

# Look up my favorite song

song_link = browser.get_link('trains')

browser.follow_link(song_link)

# Can also search HTML using regex patterns

lyrics = browser.find(class_=re.compile(r'\blyrics\b'))

lyrics.text # \nTrain set and match spied under the blind...

RoboBrowser combines the best of two excellent Python libraries:

Requests

and

BeautifulSoup

. RoboBrowser

represents browser sessions using Requests and HTML responses using

BeautifulSoup, transparently exposing methods of both libraries:

import re

from robobrowser import RoboBrowser

browser = RoboBrowser(user_agent='a python robot')

browser.open('https://github.com/')

# Inspect the browser session

browser.session.cookies['_gh_sess'] # BAh7Bzo...

browser.session.headers['User-Agent'] # a python robot

# Search the parsed HTML

browser.select('div.teaser-icon') # [

#

#

,

# ...

browser.find(class_=re.compile(r'column', re.I)) #

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值