-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
27 lines (24 loc) · 818 Bytes
/
scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import requests
from bs4 import BeautifulSoup
def scrape():
url = "http://www.straitstimes.com/singapore"
r = requests.get(url)
page = BeautifulSoup(r.text,'html.parser')
headlines = page.find_all(class_="story-headline")
imgs = page.find_all('img',class_='image-style-retina_large')
#get all the headline
for headline in headlines:
print (headline.get_text())
#get all headlineurl
headlineurl = headline.find('a')
print ("http://straitstimes.com/{0}".format(headlineurl.get('href')))
#get all imgurl of the headlines
i = 0
for img in imgs:
#only get img with odd array position, even array position are thumbnails
if i%2==1:
print (img)
imglink = img['src']
#imglink
i=i+1
scrape()