資料採集與融合技術第一次實踐作業

lzwang發表於2024-10-15

一、用requests和BeautifulSoup庫方法定向爬取給定網址(http://www.shanghairanking.cn/rankings/bcur/2020)的資料,螢幕列印爬取的大學排名資訊

1、核心程式碼與效果圖展示

  • 核心程式碼
# 目標網址
url = "http://www.shanghairanking.cn/rankings/bcur/2020"

# 獲取網頁內容
response = urllib.request.urlopen(url)
web_content = response.read()

# 使用BeautifulSoup解析網頁
soup = BeautifulSoup(web_content, 'html.parser')

# 查詢包含排名資訊的表格或元素
table = soup.find('table')  # 假設排名資訊在一個表格裡

# 表頭
print("排名  學校名稱   省市   學校型別   總分")
  • 效果圖展示

2.心得體會

透過本次作業,我認識到學習程式設計技術的必要性。在當今資訊時代,資料獲取和分析能力的重要性不言而喻。掌握爬蟲技術,能夠幫助我們快速獲取所需資訊,提高工作效率。同時,這也激發了我繼續深入學習程式設計的興趣和動力。爬蟲技術在現實生活中具有廣泛的應用場景。本次作業讓我親身體驗到了爬蟲技術的實用性。透過短短几行程式碼,我們就能夠從網站上獲取到所需的大學排名資訊,這對於學術研究、教育諮詢等領域具有很高的價值。

二、用requests和re庫方法設計某個商城(自已選擇)商品比價定向爬蟲,爬取該商城,以關鍵詞“書包”搜尋頁面的資料,爬取商品名稱和價格

1、核心程式碼與效果圖展示

  • 核心程式碼
#request獲取網頁資料
url=r'http://search.dangdang.com/?key=%B1%CA%BC%C7%B1%BE%B5%E7%C4%D4&act=input'
try:
    req = requests.get(url)
    req.raise_for_status()
    req.encoding=req.apparent_encoding
except:
    print("error im request")

data=req.text
#獲取商品資訊部分資料(在<ul></ul>)裡
match=re.search('''<ul class="bigimg cloth_shoplist" id="component_59">.*</ul''',data)
#start,end分別匹配每一件商品資訊的開頭和結尾(單件商品資訊在<li></li>裡)
data=data[match.start():match.end()]
start=re.search('<li',data)
end=re.search('</li',data)
i=0
while end!=None:
    #獲取單件商品的資訊my_data
    my_data=data[start.end():end.start()]
    #獲取商品的price
    price_sta_match=re.search('<span class="price_n">',my_data)
    price_end_match=re.search('</span>',my_data)
    price=my_data[price_sta_match.end()+5:price_end_match.start()]
    #獲取商品name
    title_match=re.search('title="',my_data)
    ddclick_match=re.search('ddclick',my_data)
    name=my_data[title_match.end():ddclick_match.start()].strip()
    name=name[:-1]
    #輸出編號,price,name
    print(i,price,name,sep='\t')
  • 效果圖展示

2、心得體會

透過這次作業,我對Python網路爬蟲技術有了更深入的瞭解。尤其是在使用requests庫和re庫進行網頁資料請求和解析的過程中,我學會了如何高效地獲取目標資訊。同時,這次實踐也鍛鍊了我的程式設計能力和解決問題的能力。

在爬取資料的過程中,正規表示式發揮了關鍵作用。它能夠幫助我們快速地從複雜的HTML文件中提取所需資訊。然而,正規表示式的編寫也具有一定的挑戰性,需要細心和耐心。透過這次實踐,我更加認識到熟練掌握正規表示式的重要性。

三、爬取一個給定網頁( https://xcb.fzu.edu.cn/info/1071/4481.htm )或者自選網頁的所有JPEG和JPG格式檔案

1、核心程式碼與效果圖展示

  • 核心程式碼
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
DIR_NAME = os.path.join(BASE_PATH, "img")
if not os.path.exists(DIR_NAME):
    os.makedirs(DIR_NAME)

response = requests.get(
    url="https://news.fzu.edu.cn/info/1011/38566.htm",
    headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    }
)

# 正則匹配
pattern = r'src="(/__local/[^"]+\.jpg)"'
img_src_list = re.findall(pattern, response.text, re.S)
# 持久化儲存
for url in img_src_list:
    url_path = f"https://news.fzu.edu.cn/{url}"
    res = requests.get(
        url=url_path,
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        }
    )
    url_name = url.split('/')[-1]
    file_path = os.path.join(DIR_NAME, url_name)
    with open(file_path, mode="wb") as fj:
        fj.write(res.content)
  • 效果圖展示

2、心得體會

透過這次作業,我對Python網路爬蟲技術有了更加深入的瞭解。尤其是在使用requests庫和正規表示式進行網頁資料請求和解析的過程中,我學會了如何高效地獲取目標網頁上的圖片資源。這次作業讓我認識到,掌握網路爬蟲技術對於資料採集和資訊獲取具有重要意義

相關文章