Urllib2 получить конкретный элемент - PullRequest
1 голос
/ 01 апреля 2019

У меня есть веб-страница, и я хочу получить элемент <div class="password">, используя urllbi2 в Python без использования Beautiful Soup.

Пока мой код:

import urllib.request as urllib2
link = "http://www.chiquitooenterprise.com/password"
response = urllib2.urlopen('http://www.chiquitooenterprise.com/')
contents = response.read('password')

выдает ошибку.

Ответы [ 2 ]

2 голосов
/ 01 апреля 2019

Вам нужно decode() ответить, указав utf-8, как указано на вкладке Network:

out

Следовательно

import urllib.request as urllib2
link = "http://www.chiquitooenterprise.com/password"
response = urllib2.urlopen('http://www.chiquitooenterprise.com/')
output = response.read().decode('utf-8')
print(output)

OUTPUT

YOIYEDGXPU
1 голос
/ 01 апреля 2019

Вы не хотите, чтобы вы сказали bs4, но вы могли бы использовать запросы

import requests

r = requests.get('http://www.chiquitooenterprise.com/password')
print(r.text)
...