Python爬虫100例教程:简单的爬虫:
Python爬虫100例教程:
1、简单的爬虫:
代码:
import requests
url = 'http://www.example.com'
response = requests.get(url)
if response.status_code == 200:
print(response.text)
2、使用正则表达式提取数据:
代码:
import re
pattern = re.compile(r'
match = pattern.search(response.text)
if match:
print(match.group(1))
3、使用BeautifulSoup解析HTML:
代码:
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, 'html.pr')
title = soup.find('title')
if title:
print(title.text)
4、使用Selenium模拟浏览器:
代码:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
title = driver.find_element_by_tag_name('title')
if title:
print(title.text)
5、使用Sy框架:
代码:
import sy
class MySpider(sy.Spider):
name = 'myspider'
start_urls = [url]
def p(self, response):
title = response.xpath('//title/text()').extract_first()
if title:
print(title)
以上就是Python爬虫100例教程的一部分,更多的示例可以参考官方文档。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(17条)