用python爬取网页http://124.95.133.164/work/xjlp/new_building.jsp到表格
时间: 2023-11-16 20:05:18 浏览: 37
好的,可以使用Python中的requests和BeautifulSoup库来实现网页爬取和数据提取。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 发送GET请求并获取网页内容
url = 'http://124.95.133.164/work/xjlp/new_building.jsp'
response = requests.get(url)
html = response.content
# 使用BeautifulSoup解析网页内容并提取表格数据
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', {'class': 'table'})
rows = table.find_all('tr')
data = []
for row in rows:
cols = row.find_all('td')
cols = [col.text.strip() for col in cols]
data.append(cols)
# 将数据保存到DataFrame中并输出到控制台
df = pd.DataFrame(data)
print(df)
```
这段代码会将从网页中提取的表格数据保存到一个DataFrame中,并输出到控制台。你可以根据需要对输出格式进行调整。
相关问题
用python爬取http://124.95.133.164/work/xjlp/new_building.jsp
好的,您想要用Python爬取http://124.95.133.164/work/xjlp/new_building.jsp这个网页的内容是吗?使用Python进行爬虫可以使用第三方库requests和BeautifulSoup来实现。首先需要安装这两个库,可以使用以下命令进行安装:
```
pip install requests
pip install beautifulsoup4
```
接着,可以使用以下代码获取网页内容并解析:
```python
import requests
from bs4 import BeautifulSoup
url = "http://124.95.133.164/work/xjlp/new_building.jsp"
response = requests.get(url)
html = response.content.decode('utf-8')
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())
```
这段代码首先使用requests库获取网页内容,然后使用BeautifulSoup把HTML代码解析为可读性更高的形式。通过print(soup.prettify())可以输出整个网页的HTML代码。
python爬取网页http://124.95.133.164/work/xjlp/new_building.jsp的数据并写入Excel
好的,我可以帮您解决这个问题。您可以使用 Python 中的 requests 库和 BeautifulSoup 库来爬取网页数据,然后使用 pandas 库将数据写入 Excel 文件中。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 发送 HTTP 请求
url = 'http://124.95.133.164/work/xjlp/new_building.jsp'
response = requests.get(url)
# 解析 HTML 数据
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table')
# 将数据存储到 DataFrame 中
df = pd.read_html(str(table))[0]
# 将数据写入 Excel 文件中
writer = pd.ExcelWriter('data.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
```
这段代码会将网页 http://124.95.133.164/work/xjlp/new_building.jsp 上的表格数据写入名为 data.xlsx 的 Excel 文件中。您可以根据需要修改文件名和表格名称。