Python编写爬虫的基本语法包括以下几个步骤和要点:
导入模块
使用`import`语句导入所需的模块,如`requests`、`BeautifulSoup4`等。
```python
import requests
from bs4 import BeautifulSoup
发送HTTP请求
使用`requests`模块中的`get()`或`post()`方法发送HTTP请求。
```python
response = requests.get('http://example.com')
解析网页
使用`BeautifulSoup`解析网页源代码,提取所需数据。
```python
soup = BeautifulSoup(response.text, 'html.parser')
提取数据
使用`find()`和`find_all()`方法提取网页中的数据。
```python
查找第一个匹配的元素
element = soup.find('div', class_='example')
查找所有匹配的元素
elements = soup.find_all('div', class_='example')
处理数据
```python
text = element.get_text()
条件语句和循环语句
使用`if`、`else`、`elif`和`for`、`while`等控制程序流程。
```python
if age > 18:
print('I am an adult.')
else:
print('I am not an adult.')
多线程爬虫
使用`threading`模块实现多线程爬取。
```python
import threading
def crawl_page(url):
response = requests.get(url)
print(f'Crawled {url}, status code: {response.status_code}')
urls = ['http://example.com/page1', 'http://example.com/page2']
threads = []
for url in urls:
thread = threading.Thread(target=crawl_page, args=(url,))
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
使用代理IP
在开发网络爬虫时,可以使用代理IP绕过IP封锁。
```python
proxies = {
'http': 'http://127.0.0.1:8080',
'https': 'http://127.0.0.1:8080',
}
response = requests.get('http://example.com', proxies=proxies)
以上是Python爬虫的基本语法。请根据实际需求调整代码,并注意遵守网站的爬虫协议和法律法规。