python - 如果 [string] 中的 [string] 不适用于请求的网页文本

标签 python screen-scraping

我正在尝试打开一个网页并将一些字符串从其中抓取到列表中。该列表最终将由网页上显示的所有名称填充。为了尝试这样做,我的代码如下所示:

    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()

如您所见,我想做的就是从请求操作返回的文本中获取以以下字符开头的行:

最佳答案

我不知道你到底想做什么,但这没有任何意义:

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 ?这是开玩笑吗?

关于python - 如果 [string] 中的 [string] 不适用于请求的网页文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27145082/

相关文章:

python - 使用 pandas 系列元素作为边界的简洁/优雅集成

python - 在 Windows 10 Pro 上运行 remote = False 时出现 GEKKO 错误

c# - 在线程中执行 Webbrowser 控件的屏幕显示

java - 弱类型、自动装箱、扩大转换之间有什么区别?

php - 用于文件/图像操作的有效解释型编程语言

python - 反转字符串顺序

perl - 如何使用 Perl 进行屏幕抓取?

python - 在 Scrapy 蜘蛛中动态添加到 allowed_domains

javascript - 使用 Apify 进行网页抓取

selenium - 如何使用 Selenium WebDriver 进行可行的 Web 冒烟测试?