作业①
实验内容
要求
指定一个网站,爬取这个网站中的所有的所有图片,例如:中国气象网(http://www.weather.com.cn)。使用scrapy框架分别实现单线程和多线程的方式爬取。
–务必控制总页数(学号尾数2位)、总下载的图片数量(尾数后3位)等限制爬取的措施。
输出信息
将下载的Url信息在控制台输出,并将下载的图片存储在images子文件中,并给出截图。
代码
通过修改 settings.py 中的 CONCURRENT_REQUESTS 可以指定REQUESTS的最大数量。
# spider
class WeatherSpider(scrapy.Spider):
name = "weather"
allowed_domains = ["www.weather.com.cn","pic.weather.com.cn"]
start_urls = ["http://www.weather.com.cn","http://www.weather.com.cn/life/"]
count = 0
limit = 102
def parse(self, response):
sel = scrapy.Selector(response)
if self.count >= 102:
return
# 爬取所有图片url
images = sel.xpath('//img/@src')
for image in images:
url = image.extract()
items = GetImagesItem()
items["url"] = url
print(url)
self.count += 1
yield items
# pipelines
class DownloadImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
yield Request(item["url"])
def file_path(self, request, response=None, info=None, item=None):
return os.path.join('images', request.url.split('/')[-1])
结果


实验心得
通过这个实验,我学会了如何使用Scrapy框架进行网站爬取,并掌握了限制爬取的方法。我对网页爬取的流程和技术有了更深入的了解,并掌握了如何处理和存储爬取到的数据。
作业②
实验内容
要求
熟练掌握 scrapy 中 Item、Pipeline 数据的序列化输出方法;使用scrapy框架+Xpath+MySQL数据库存储技术路线爬取股票相关信息。
候选网站:东方财富网:https://www.eastmoney.com/
输出信息
MySQL数据库存储和输出格式如下:
表头英文命名例如:序号id,股票代码:bStockNo……,由同学们自行定义设计。
| 序号 | 股票代码 | 股票名称 | 最新报价 | 涨跌幅 | 涨跌额 | 成交量 | 振幅 | 最高 | 最低 | 今开 | 昨收 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 688093 | N世华 | 28.47 | 10.92 | 26.13万 | 7.6亿 | 22.34 | 32.0 | 28.08 | 30.20 | 17.55 |
代码
截取核心部分对股票名称的提取,pipelines数据库连接部分省略。该网站使用动态数据,因此使用Selenium进行提取。
def parse(self, response):
# 使用 Selenium 获取动态加载的内容
self.driver.get(response.url)
sel = scrapy.Selector(text=self.driver.page_source)
tables = sel.css("#table_wrapper-table")
tbody = tables.xpath(".//tbody")
trs = tbody.xpath(".//tr")
for line in trs:
name = line.css(".mywidth a::text").extract_first()
...
items = MoneyItem()
items["name"] = name
...
yield items
next_page = self.driver.find_element(By.CSS_SELECTOR, '#main-table_paginate > a.next.paginate_button')
# 翻页爬取,如果class不包含disabled,说明还有下一页
if "disabled" not in next_page.get_attribute("class"):
next_page.click()
yield scrapy.Request(url=self.driver.current_url, callback=self.parse)
else:
self.driver.quit()
结果

数据库文件

实验心得
在这个实验中,我学会了综合运用Scrapy框架、XPath和MySQL数据库,同时还引入了Selenium来处理动态网页。我不仅熟悉了Scrapy框架的使用,还学会了通过XPath选择器提取所需数据,并将其存储到MySQL数据库中,体会到了Scrapy的不足与其解决方案。
作业③
实验内容
要求
熟练掌握 scrapy 中 Item、Pipeline 数据的序列化输出方法;使用scrapy框架+Xpath+MySQL数据库存储技术路线爬取外汇网站数据。
候选网站:中国银行网:https://www.boc.cn/sourcedb/whpj/
输出信息
| Currency | TBP | CBP | TSP | CSP | Time |
|---|---|---|---|---|---|
| 阿联酋迪拉姆 | 198.58 | 192.31 | 199.98 | 206.59 | 11:27:14 |
代码
同②截取核心部分对货币名称的提取,pipelines数据库连接部分省略。不同的是翻页处理是通过构造url完成的,代码也一并给出。
def start_requests(self):
for page in range(0, 10):
if page == 0:
url = "https://www.boc.cn/sourcedb/whpj/"
else:
url = f"https://www.boc.cn/sourcedb/whpj/index_{page}.html"
yield Request(url=url, callback=self.parse)
def parse(self, response):
# 使用 Selenium 获取动态加载的内容
self.driver.get(response.url)
sel = scrapy.Selector(text=self.driver.page_source)
table = sel.xpath("/html/body/div/div[5]/div[1]/div[2]/table")
tbody = table.xpath(".//tbody")
trs = tbody.xpath(".//tr")
# 第一行为表头要跳过
for line in trs[1:]:
currency = line.xpath("td[1]/text()").extract_first()
...
items = BocItem()
items["currency"] = currency
...
yield items
结果

数据库文件

实验心得
与实验2类似,使用了Scrapy框架、XPath和MySQL数据库存储技术路线来爬取外汇网站数据。我更加了解了Scrapy中Item和Pipeline数据的序列化输出方法。我意识到了爬虫开发需要综合运用各种技术和工具,并在实践中不断调试和优化。这些体会对我今后在数据爬取和处理方面的工作具有很大的指导意义。