使用python3.3.3从受密码保护的网页检索数据

2024-03-29 14:42:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用urllib访问一个网站,然后剥离页面源代码,以便从中收集一些数据。我知道如何为公共网站做到这一点,但我不知道如何使用urllib来为受密码保护的网页做到这一点。我知道用户名和密码,只是对如何让urllib放入正确的凭据,然后将我重新路由到要从中删除数据的正确页面感到非常困惑。目前,我的代码是这样的。问题是它正在调出登录页的源代码。你知道吗

from tkinter import *
import csv
from re import findall
import urllib.request
    def info():    

        file = filedialog.askopenfilename()
        fileR = open(file, 'r')
        hold = csv.reader(fileR, delimiter=',', quotechar='|')
        aList=[]
        for item in hold:
            if item[1] and item[2] == "":
                print(item[1])
                url = "www.example.com/id=" + item[1]
                request = urllib.request.urlopen(url)
                html = request.read()
                data = str(html)
                person = findall('''\$MainContent\$txtRecipient\"\stype=\"text\"\svalue=\"([^\"]+)\"''',data)
            else:
                pass

        fileR.close

记住,我使用的是python3.3.3。任何帮助都将不胜感激!你知道吗


Tags: csv数据fromimporturl源代码网站request