Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chapter5/1-getPageMedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def getDownloadPath(baseUrl, absoluteUrl, downloadDirectory):
fileUrl = getAbsoluteURL(baseUrl, download["src"])
if fileUrl is not None:
print(fileUrl)

urlretrieve(fileUrl, getDownloadPath(baseUrl, fileUrl, downloadDirectory))
fileUrl = fileUrl.split('?')[0]
urlretrieve(fileUrl, getDownloadPath(baseUrl, fileUrl, downloadDirectory))
8 changes: 5 additions & 3 deletions chapter5/2-createCsv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import csv
#from os import open
import os

csvFile = open("../files/test.csv", 'w+')
filename = "../files/test.csv"
os.makedirs(os.path.dirname(filename), exist_ok=True)
csvFile = open(filename, 'w', newline='')
try:
writer = csv.writer(csvFile)
writer.writerow(('number', 'number plus 2', 'number times 2'))
for i in range(10):
writer.writerow( (i, i+2, i*2))
writer.writerow((i, i+2, i*2))
finally:
csvFile.close()
18 changes: 9 additions & 9 deletions chapter5/3-scrapeCsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

html = urlopen("http://en.wikipedia.org/wiki/Comparison_of_text_editors")
bsObj = BeautifulSoup(html)
#The main comparison table is currently the first table on the page
table = bsObj.findAll("table",{"class":"wikitable"})[0]
# The main comparison table is currently the first table on the page
table = bsObj.findAll("table", {"class": "wikitable"})[0]
rows = table.findAll("tr")

csvFile = open("files/editors.csv", 'wt')
csvFile = open("../files/editors.csv", 'w', encoding='utf-8', newline='')
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = []
for cell in row.findAll(['td', 'th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
for row in rows:
csvRow = []
for cell in row.findAll(['td', 'th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
csvFile.close()
3 changes: 2 additions & 1 deletion chapter5/5-storeWikiLinks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import datetime
import random
import pymysql
Expand Down Expand Up @@ -30,4 +31,4 @@ def getLinks(articleUrl):
links = getLinks(newArticle)
finally:
cur.close()
conn.close()
conn.close()
11 changes: 0 additions & 11 deletions files/test.csv

This file was deleted.