怎么使用python爬取东西
时间: 2024-03-21 17:33:23 浏览: 56
要使用Python爬取东西,需要以下步骤:
1. 确定网站的URL和需要爬取的数据
2. 导入必要的库,如requests、BeautifulSoup等
3. 使用requests库发送HTTP请求,获取网页源代码
4. 使用BeautifulSoup库解析网页源代码,提取所需的数据
5. 存储数据,如写入文件或存入数据库
以下是一个简单的Python爬虫示例,用于爬取维基百科的Python词条页面,并提取其中的表格数据:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求,获取网页源代码
url = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析网页源代码,提取表格数据
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', {'class': 'wikitable'})
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
if len(cells) > 0:
print(cells[0].text, cells[1].text)
```
该示例使用requests库发送HTTP请求,获取维基百科Python词条页面的网页源代码。然后使用BeautifulSoup库解析网页源代码,找到词条页面中的表格,并提取其中的数据,最后将数据输出到控制台。
阅读全文
相关推荐














