[Solved]Python Beautifulsoup4 Question Want Append New Column Csv File Cannot Author Column Autom Q37121406
I have a python beautifulsoup4 question. I want to append a newcolumn in my CSV file but cannot do it. The author column wasautomatically appended below the title column. Can anyone fix it,and detail an efficient way?
the link is here: https://repl.it/repls/SnoopyChiefVariety
/——————————————————-/
import requests
import csv
from bs4 import BeautifulSoup
row = [‘Title’, ‘Link’, ‘Author’, ‘Time’, ‘Reply’, ‘Read’]
pages = []
with open(‘chineseinsfbay.csv’, ‘a’, encoding=’utf-8′, newline=”)as writeFile:
f = csv.writer(writeFile)
f.writerows([row])
# automate change to other pages, number 100 can be any otherintegers
for i in range(0,105,15):
url = ‘https://www.chineseinsfbay.com/f/page_viewforum/f_29/start_’+ str(i) + ‘.html’
pages.append(url)
# one page per times
for item in pages:
# Collect and parse page
page = requests.get(item)
soup = BeautifulSoup(page.text, ‘html.parser’)
# Pull all text from the div class
info_list = soup.find(class_=’forum_left’)
# Pull text from all instances of tag within div class
info_list_title = info_list.find_all(‘a’, class_=’title’)
# Pull author from all instances of tag within div class
info_list_author = info_list.find_all(‘span’,class_=’author’)
# Pull time from all instances of tag within div class
info_list_time = info_list.find_all(‘span’, class_=’time’)
# Pull reply count from all instances of tag within div class
info_list_reply = info_list.find_all(‘span’,class_=’reply_count’)
# Pull read count from all instances of tag within div class
info_list_read = info_list.find_all(‘span’,class_=’read_count’)
# Print all titles and links
for title in info_list_title:
titles = title.contents[0]
links = ‘https://www.chineseinsfbay.com’ + title.get(‘href’)
f.writerow([titles,links])
for author in info_list_author:
authors = author.contents[0]
f.writerow([authors])
writeFile.close()
Expert Answer
Answer to I have a python beautifulsoup4 question. I want to append a new column in my CSV file but cannot do it. The author colum… . . .
OR

