网络爬虫的第一步是发送HTTP请求,获取网页内容。Python的requests库是进行HTTP请求的强大工具。
import requests
url = 'https://www.example.com'
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200: print("网页内容获取成功") print(response.text)
else: print("网页内容获取失败,状态码:", response.status_code)data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://www.example.com', data=data)
# 检查请求是否成功
if response.status_code == 200: print("数据提交成功") print(response.text)
else: print("数据提交失败,状态码:", response.status_code)获取到网页内容后,我们需要解析这些数据,提取出有用的信息。Python的BeautifulSoup库是一个用于解析HTML和XML文档的工具。
from bs4 import BeautifulSoup
html = 'Example
'
soup = BeautifulSoup(html, 'html.parser')
# 提取文本内容
print(soup.p.text)# 假设我们有一个更复杂的HTML结构
html = '''
Test
Item 1
Paragraph 1
Item 2
Paragraph 2
'''
soup = BeautifulSoup(html, 'html.parser')
items = soup.select('.item')
for item in items: print(item.h1.text) print(item.p.text)一些网站为了防止爬虫,会设置各种反爬虫机制,如IP封禁、验证码等。我们可以使用以下技巧来应对:
proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080',
}
response = requests.get('https://www.example.com', proxies=proxies)from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.example.com')
# 执行相关操作,如点击、填写表单等
driver.quit()提取到有用的信息后,我们需要将这些数据保存下来,以便后续分析或使用。
import csv
data = [['Name', 'Age'], ['Alice', 25], ['Bob', 30]]
with open('data.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerows(data)import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)为了保持数据的实时性,我们可以使用定时任务来定期运行爬虫。
schedule库import schedule
import time
def job(): print("任务执行中")
# 每5分钟执行一次任务
schedule.every(5).minutes.do(job)
while True: schedule.run_pending() time.sleep(1)通过以上五大绝招,您已经掌握了Python获取网页数据的技巧。现在,您可以轻松地开始您的网络爬虫之旅,探索网络上的宝藏!