我正在尝试打开一个网页并将其中的一些字符串写入列表中。该列表最终将由网页上显示的所有名称填充。在尝试这样做时,我的代码如下所示:
import xlsxwriter, urllib.request, string, http.cookiejar, requests
def main():
username = 'john.mauran'
password = 'fZSUME1q'
log_url = 'https://aries.case.com.pl/'
dest_url = 'https://aries.case.com.pl/main_odczyt.php?strona=eksperci'
login_values = {'username' : username , 'password' : password }
r = requests.post(dest_url, data=login_values, verify=False, allow_redirects=False)
open_sesame = r.text
#reads the expert page
readpage_list = open_sesame.splitlines()
#opens up a new file in excel
workbook = xlsxwriter.Workbook('expert_book.xlsx')
#adds worksheet to file
worksheet = workbook.add_worksheet()
#initializing the variable used to move names and dates
#in the excel spreadsheet
boxcoA = ""
boxcoB = ""
#initializing expert attribute variables and lists
url_ticker = 0
name_ticker = 0
raw_list = []
url_list = []
name_list= []
date_list= []
#this loop goes through and finds all the lines
#that contain the expert URL and name and saves them to raw_list::
#raw_list loop
for i in open_sesame:
if '<tr><td align=left><a href=' in i:
raw_list += i
if not raw_list:
print("List is empty")
if raw_list:
print(raw_list)
main()
正如您所看到的,我想要做的就是从请求操作返回的文本中取出行,这些行以下列字符开头。
答案 0 :(得分:1)
我不确切地知道你要做什么,但这没有任何意义:
for i in open_sesame:
if '<tr><td align=left><a href=' in i:
raw_list += i
首先,如果迭代open_sesame
,这是一个字符串,迭代中的每个项都将是字符串中的一个字符。然后'<tr><td align=left><a href=' in i
将始终为假。
其次,raw_list += i
不是您将项目附加到列表的方式。
最后,为什么变量名为open_sesame
?这是个玩笑吗?