在Python中,你可以使用以下步骤将爬虫载入文档:
安装BeautifulSoup库
pip3 install beautifulsoup4
载入HTML文档
使用`requests`库获取HTML文档。
import requests
html = requests.get('http://example.com').text
解析HTML文档
使用BeautifulSoup解析HTML文档。
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
查找和提取数据
使用BeautifulSoup查找和提取所需的HTML元素。
elements = soup.find_all('div', class_='myclass')
格式化结果
将提取的数据格式化为适合载入文档的格式,如JSON或CSV。
示例:将数据写入JSON文件
import json
with open('output.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
示例:将数据写入CSV文件
import csv
with open('output.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['header1', 'header2'])
writer.writerow([data1, data2])
将爬虫代码导出到文件
在Python IDE中创建并编写爬虫代码,然后保存文件,指定文件路径和文件名。
示例:将HTML内容写入文本文件
with open('rawcodes.txt', 'wb') as f:
f.write(html_1)
确保你有对文件路径的写权限,并选择合适的文件模式(如'w'表示写入,'a'表示追加)。
以上步骤可以帮助你记录爬取过程、结果和分析。