Python 3 网页转行

标签 python python-3.x web-scraping

我尝试使用 python 3 从网络上读取内容,然后逐一打印所有行。

到目前为止我看到的最好的方法是使用 urllib.request 并做这样的事情:

import urllib.request
url_target = urllib.request.urlopen("http://stackoverflow.com")
tmp_copy_string = url_target.read().decode("utf8")
file = "file"
for word in tmp_copy_string:
        print(word)

我以为这段代码逐字打印 - 它不......

问题不仅在于它不逐字打印,而且逐字符打印......

有没有逐行打印的好方法?

不使用额外的库。

最佳答案

您可以通过\n拆分它:

import urllib.request

url_target = urllib.request.urlopen("http://stackoverflow.com")
tmp_copy_string = url_target.read().decode("utf8").split('\n')    #split string on newline

for line in tmp_copy_string:
        print(line)

这将逐行打印代码

关于Python 3 网页转行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23777998/

相关文章:

python - 如何将 __repr__ 与多个参数一起使用?

python-3.x - 带有MSG_DONTWAIT的Python socket.recv

python - 如何强制自己更优雅地编码?

python - 如何使用 Selenium 和 BeautifulSoup 进行 for 循环

python - 按 Fortran 连续顺序 reshape numpy.array

python - flask-sqlalchemy where in 子句查询语法是什么?

python - 统一码编码错误 : 'cp949' codec can't encode character '\u20a9' in position 90: illegal multibyte sequence

python - 从三个一维数组制作一个 3d python 数组

Python 从多个页面请求 .get()?

python - 刮过无限滚动条