Python: Read URL

From OnnoWiki
Jump to navigation Jump to search
pip3 install urllib
pip3 install requests
pip3 install BeautifulSoup4

Contoh 1

import requests
from BeautifulSoup import BeautifulSoup

link = "http://www.somesite.com/details.pl?urn=2344"
f = requests.get(link)
print(f.text)
soup = BeautifulSoup(f)
all_text = .join(soup.findAll(text=True))
print(all_text)


Contoh 2

import requests
import urllib.request
from bs4 import BeautifulSoup

import urllib
from bs4 import BeautifulSoup

url = "https://www.industry.co.id/read/49900/dongkrak-penjualan-tam-hadirkan-toyota-driving-experience"
with urllib.request.urlopen(url) as url:
    html = url.read()
soup = BeautifulSoup(html)

# kill all script and style elements
for script in soup(["script", "style"]):
    script.extract()    # rip it out
# get text
text = soup.get_text()

# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
print(text)

Pranala Menarik