iT邦幫忙

0

爬蟲擷取小問題

https://ithelp.ithome.com.tw/upload/images/20191210/20116868XFU07T7GwH.png
圖中有兩張照片 我想要每一張照片的連結
請問我該怎麼擷取並存入list

知道正則是[a-zA-z]+://[^\s]*.jpeg
但不知道要怎麼擷取 請教一下大大
或是有正則以外的用法 用屬性來找的 也麻煩指導一下 謝謝

米歐 iT邦新手 3 級 ‧ 2019-12-10 17:40:31 檢舉
語言是?
echochio iT邦高手 1 級 ‧ 2019-12-10 18:29:52 檢舉
是 python 嗎 ?
其他語言也可擷取 的 ... ....
是的 python 不好意思沒講
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
froce
iT邦大師 1 級 ‧ 2019-12-11 00:35:31

因為你沒寫要抓什麼網頁...所以隨便選一個了。
語言是python。

首先要裝 requests-html

from requests_html import HTMLSession

url = "https://www.google.com/search?q=google&client=ubuntu&hs=Jw5&channel=fs&sxsrf=ACYBGNQoBB1ys9AAPy5g3glvXTdn8PWs7Q:1575993930420&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiT7Y_zuqvmAhUIq5QKHdSHBcoQ_AUoAnoECAwQBA&biw=1920&bih=951"
r = HTMLSession().get(url)
imgs = r.html.find("img")

感謝大大
那擷取後會得到像這樣的
<Element 'img' src='/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' alt='Google' height='30' width='92' onload="typeof google==='object'&&google.aft&&google.aft(this)">
不乾淨 有其他能指定網址尾端是png或jpeg的方法嗎

Han iT邦研究生 1 級 ‧ 2019-12-12 10:36:40 檢舉

抓到這坨字之後使用正規表示法抓出來囉!
不過沒用過python不太清楚,看了一下應該是

import re
re.search(pattern, string)

這樣就可以擷取到你想要的網址了吧!

不過python應該也有像其他語言寫的套件
直接取得element的attribute內的src吧!
這部份就等其他大大回囉~

froce iT邦大師 1 級 ‧ 2019-12-12 10:56:19 檢舉

requests-html是一個高級的爬蟲lib包,爬出來的是他的Element物件。
以上面的例子,你可以很簡單的存取imgs下的所有src:

for img in imgs:
    src = img.attrs.get("src")

請參閱 requests-html 給的範例和API,很短。

0
wesley41616
iT邦新手 5 級 ‧ 2019-12-12 17:59:55

以 ettoday 熱門新聞為例:

import requests
from bs4 import BeautifulSoup

url = 'https://www.ettoday.net/news/hot-news.htm'
res = requests.get(url).text
doc = BeautifulSoup(res, 'lxml')

for news in doc.select('.piece'):
    image = news.findAll('img')[0]['src']
    print(image)

https://ithelp.ithome.com.tw/upload/images/20191212/201184350IPsAL0Vzc.png

alex0831 iT邦新手 5 級 ‧ 2020-07-23 00:12:32 檢舉

這個線上教學有你需要的做法,我自己看了之後很有幫助,課程評價非常好,應該可以解決你的問題!

https://www.udemy.com/course/python-crawler/?referralCode=A4F2B9D20A2C35D5001D

我要發表回答

立即登入回答